【问题标题】:HTML Move Canvas object with onclick带有 onclick 的 HTML 移动画布对象
【发布时间】:2017-11-30 16:58:04
【问题描述】:

是否可以附加onclick事件来左右移动对象img

按钮为占位符,顺序为左下右。

我面临两个问题:-

1. 我的宽度是500px,即使我在250px 绘制它。它没有居中。
2.我尝试了多种方法将onclick功能添加到-/+y-axisx-axis

我什至尝试过适应这段代码:
http://www.authorcode.com/how-to-move-an-object-on-the-html5-canvas/

但也没有运气。 有什么我想念的吗?我在 google 上查过的主要是 keyevent 处理程序。

JSBIN:

http://jsbin.com/wuseguvuka/2/edit?html,css,output

CSS:

@charset "utf-8";
/* Global Styles */
#main {
    width:100%;
    background-color:red;
}   
body {
    margin-left: auto;
    margin-right: auto;
    width: 92%;
    max-width: 960px;
    padding-left: 2.275%;
    padding-right: 2.275%;
}
/* Header */
header {
    text-align: center;
    display: block;
}
header .profileLogo .logoPlaceholder {
    background-color: white;
    color:black;
    width: 300px;
    text-align: center;
}
header .logoPlaceholder span {
    width: 180px;
    height: 22px;
    font-family: 'Montserrat', sans-serif;
    color: black;
    font-size: 30px;
    font-weight: 700;
    line-height: 53px;
}
header .logoPlaceholder2 span {
    width: 180px;
    height: 22px;
    font-family: 'Montserrat', sans-serif;
    color: black;
    font-size: 30px;
    font-weight: 700;
    line-height: 53px;
}
header .profilePhoto {
    background-color: rgba(237,237,237,1.00);
    width: 259px;
    border-radius: 50%;
    height: 259px;
    clear: both;
}
header .profileHeader h1 {
    font-family: 'Montserrat', sans-serif;
    color: rgba(146,146,146,1.00);
    font-size: 30px;
    font-weight: 700;
    line-height: 24px;
}
header .profileHeader h3 {
    font-family: sans-serif;
    color: rgba(146,146,146,1.00);
    font-size: 16px;
    font-weight: 700;
    line-height: 24px;
}
hr {
    background-color: rgba(208,207,207,1.00);
    height: 1px;
}
header .profileHeader p {
    font-family: sans-serif;
    color: rgba(146,146,146,1.00);
    font-size: 14px;
    font-weight: 400;
    line-height: 1.8;
    overflow-y: auto;
}
header .socialNetworkNavBar {
    text-align: center;
    display: block;
    margin-top: 60px;
    clear: both;
}
header .socialNetworkNavBar .socialNetworkNav {
    border-radius: 50%;
    cursor: pointer;
}
header .socialNetworkNavBar .socialNetworkNav img:hover {
    opacity: 0.5;
}


/* Media query for Mobile devices*/
@media only screen and (min-width : 285px) and (max-width : 480px) {
/* Header */
#main {

width:100%;

}   
header {
    width: 100%;
    float: left;
}
.wrapper {
   display: inline-block;
   width: 100%;
   height: 100%;
}   
header .profileLogo .logoPlaceholder {
    display: inline-block;
    text-align: left;
}
header .profilePhoto {
    display: inline-block;
}
header .profileHeader {
    text-align: center;
    margin-top: 10%;
}
header .profileHeader p {
    text-align: justify;
}
header .socialNetworkNavBar {
    text-align: center;
    display: block;
    margin-top: 60px;

    clear: both;
    margin-bottom: 15%;
}
header .socialNetworkNavBar .socialNetworkNav {
    width: 60px;
    height: 60px;
    display: inline-block;
    margin-right: 23px;
}

}

/* Media Query for Tablets */
@media only screen and (min-width : 481px) and (max-width : 1024px) {
/* Header */
header {
    width: 100%;
    float: none;
}
header .profileLogo .logoPlaceholder {
    display: inline-block;
    font-family: 'Montserrat', sans-serif;
}
header .profilePhoto {
    float: left;
    clear: both;
    margin-right: 3%;
    margin-top: 5%;
}
header .profileHeader {
    text-align: left;
    margin-top: 7%;
}
header .socialNetworkNavBar .socialNetworkNav {
    width: 74px;
    height: 74px;
    display: inline-block;
    margin-right: 23px;
}

}

/* Desktops and laptops  */
@media only screen and (min-width:1025px) {
/* Header */
header .profileLogo .logoPlaceholder {
    float: left;
    margin-bottom: 50px;
}
header .profilePhoto {
    float: left;
    clear: both;
    margin-right: 3%;
}
header .profileHeader {
    text-align: left;
    padding-top: 10%;
}
header .socialNetworkNavBar .socialNetworkNav {
    width: 74px;
    height: 74px;
    display: inline-block;
    margin-right: 23px;
}



.wrapper {
   display: table;
   padding: 0;
   width: 100%;
   height: 100%;
}
.container {
   display: table-cell;
   vertical-align: middle;
}
.canvas-container {
   position: relative;
   max-width: 1024px;
   min-width: 120px;
   margin: 0 auto;
}



}

HTML:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>About Page template By Adobe Dreamweaver CC</title>
<link href="AboutPageAssets/styles/aboutPageStyle.css" rel="stylesheet" type="text/css">

<!--The following script tag downloads a font from the Adobe Edge Web Fonts server for use within the web page. We recommend that you do not modify it.-->
<script>var __adobewebfontsappname__="dreamweaver"</script><script src="http://use.edgefonts.net/montserrat:n4:default;source-sans-pro:n2:default.js" type="text/javascript"></script>

<script>


function load() {
 var context = document.getElementById('main').getContext("2d");

var img = new Image();
img.src = "http://via.placeholder.com/75x150";
img.onload = function () {

    context.drawImage(img, 250, 0);
}

}
</script>

</head>

<body onload="load();">
<!-- Header content -->
<header>
  <div class="profileLogo"> 
    <!-- Profile logo. Add a img tag in place of <span>. -->
    <p class="logoPlaceholder"><!-- <img src="logoImage.png" alt="sample logo"> --><span>Name :</span></p>
    <p class="logoPlaceholder"><!-- <img src="logoImage.png" alt="sample logo"> --><span>Attempts :</span></p>
    <p class="logoPlaceholder"><!-- <img src="logoImage.png" alt="sample logo"> --><span>Score :</span></p>
  </div>
  <section>
<div class="wrapper">
  <div class="container">
    <div class="canvas-container">

      <canvas id="main" width="500px" height="250px"></canvas>
    </div>
  </div>
</div>
  </section>
  <!-- Links to Social network accounts -->
  <aside class="socialNetworkNavBar">
    <div style="margin-right: 50px;" class="socialNetworkNav"> 
      <!-- Add a Anchor tag with nested img tag here --> 
      <input type="image" src="http://via.placeholder.com/75x75" >

      </div>
    <div id="left" style="margin-right: 50px;" class="socialNetworkNav"> 
      <!-- Add a Anchor tag with nested img tag here --> 
      <input type="image" src="http://via.placeholder.com/75x75" > </div>
    <div id="down" style="margin-right: 50px;" class="socialNetworkNav"> 
      <!-- Add a Anchor tag with nested img tag here --> 
      <input id="right"  type="image" src="http://via.placeholder.com/75x75" >
 </div>

  </aside>
</header>
<!-- content -->

</body>
</html>

【问题讨论】:

    标签: javascript jquery html html5-canvas


    【解决方案1】:

    是的!

    绝对有可能。以下是您如何实现这一目标...

    function load() {
       var context = document.getElementById('main').getContext("2d");
       var left = document.getElementById('left');
       var down = document.getElementById('down');
       var right = document.getElementById('right');
       var imgLoaded = false;
       var posX = 250;
       var posY = 0;
       var snap = 10;
    
       var img = new Image();
       img.onload = function() {
          imgLoaded = true;
          context.drawImage(img, posX, posY);
       };
       img.src = "http://via.placeholder.com/75x150";
    
       // left button click
       left.onclick = function() {
          if (!imgLoaded) return;
          posX = posX - snap;
          context.clearRect(0, 0, context.canvas.width, context.canvas.height);
          context.drawImage(img, posX, posY);
       };
    	
       // down button click
       down.onclick = function() {
          if (!imgLoaded) return;
          posY = posY + snap;
          context.clearRect(0, 0, context.canvas.width, context.canvas.height);
          context.drawImage(img, posX, posY);
       };
    	
       // right button click
       right.onclick = function() {
          if (!imgLoaded) return;
          posX = posX + snap;
          context.clearRect(0, 0, context.canvas.width, context.canvas.height);
          context.drawImage(img, posX, posY);
       };
    
    }
    @charset "utf-8";
    /* Global Styles */
    
    #main {
        width: 100%;
        background-color: red;
    }
    body {
        margin-left: auto;
        margin-right: auto;
        width: 92%;
        max-width: 960px;
        padding-left: 2.275%;
        padding-right: 2.275%;
    }
    /* Header */
    
    header {
        text-align: center;
        display: block;
    }
    header .profileLogo .logoPlaceholder {
        background-color: white;
        color: black;
        width: 300px;
        text-align: center;
    }
    header .logoPlaceholder span {
        width: 180px;
        height: 22px;
        font-family: 'Montserrat', sans-serif;
        color: black;
        font-size: 30px;
        font-weight: 700;
        line-height: 53px;
    }
    header .logoPlaceholder2 span {
        width: 180px;
        height: 22px;
        font-family: 'Montserrat', sans-serif;
        color: black;
        font-size: 30px;
        font-weight: 700;
        line-height: 53px;
    }
    header .profilePhoto {
        background-color: rgba(237, 237, 237, 1.00);
        width: 259px;
        border-radius: 50%;
        height: 259px;
        clear: both;
    }
    header .profileHeader h1 {
        font-family: 'Montserrat', sans-serif;
        color: rgba(146, 146, 146, 1.00);
        font-size: 30px;
        font-weight: 700;
        line-height: 24px;
    }
    header .profileHeader h3 {
        font-family: sans-serif;
        color: rgba(146, 146, 146, 1.00);
        font-size: 16px;
        font-weight: 700;
        line-height: 24px;
    }
    hr {
        background-color: rgba(208, 207, 207, 1.00);
        height: 1px;
    }
    header .profileHeader p {
        font-family: sans-serif;
        color: rgba(146, 146, 146, 1.00);
        font-size: 14px;
        font-weight: 400;
        line-height: 1.8;
        overflow-y: auto;
    }
    header .socialNetworkNavBar {
        text-align: center;
        display: block;
        margin-top: 60px;
        clear: both;
    }
    header .socialNetworkNavBar .socialNetworkNav {
        border-radius: 50%;
        cursor: pointer;
    }
    header .socialNetworkNavBar .socialNetworkNav img:hover {
        opacity: 0.5;
    }
    /* Media query for Mobile devices*/
    
    @media only screen and (min-width: 285px) and (max-width: 480px) {
        /* Header */
        
        #main {
            width: 100%;
        }
        header {
            width: 100%;
            float: left;
        }
        .wrapper {
            display: inline-block;
            width: 100%;
            height: 100%;
        }
        header .profileLogo .logoPlaceholder {
            display: inline-block;
            text-align: left;
        }
        header .profilePhoto {
            display: inline-block;
        }
        header .profileHeader {
            text-align: center;
            margin-top: 10%;
        }
        header .profileHeader p {
            text-align: justify;
        }
        header .socialNetworkNavBar {
            text-align: center;
            display: block;
            margin-top: 60px;
            clear: both;
            margin-bottom: 15%;
        }
        header .socialNetworkNavBar .socialNetworkNav {
            width: 60px;
            height: 60px;
            display: inline-block;
            margin-right: 23px;
        }
    }
    /* Media Query for Tablets */
    
    @media only screen and (min-width: 481px) and (max-width: 1024px) {
        /* Header */
        
        header {
            width: 100%;
            float: none;
        }
        header .profileLogo .logoPlaceholder {
            display: inline-block;
            font-family: 'Montserrat', sans-serif;
        }
        header .profilePhoto {
            float: left;
            clear: both;
            margin-right: 3%;
            margin-top: 5%;
        }
        header .profileHeader {
            text-align: left;
            margin-top: 7%;
        }
        header .socialNetworkNavBar .socialNetworkNav {
            width: 74px;
            height: 74px;
            display: inline-block;
            margin-right: 23px;
        }
    }
    /* Desktops and laptops  */
    
    @media only screen and (min-width: 1025px) {
        /* Header */
        
        header .profileLogo .logoPlaceholder {
            float: left;
            margin-bottom: 50px;
        }
        header .profilePhoto {
            float: left;
            clear: both;
            margin-right: 3%;
        }
        header .profileHeader {
            text-align: left;
            padding-top: 10%;
        }
        header .socialNetworkNavBar .socialNetworkNav {
            width: 74px;
            height: 74px;
            display: inline-block;
            margin-right: 23px;
        }
        .wrapper {
            display: table;
            padding: 0;
            width: 100%;
            height: 100%;
        }
        .container {
            display: table-cell;
            vertical-align: middle;
        }
        .canvas-container {
            position: relative;
            max-width: 1024px;
            min-width: 120px;
            margin: 0 auto;
        }
    }
    <!doctype html>
    <html>
    
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>About Page template By Adobe Dreamweaver CC</title>
        <link href="AboutPageAssets/styles/aboutPageStyle.css" rel="stylesheet" type="text/css">
    
        <!--The following script tag downloads a font from the Adobe Edge Web Fonts server for use within the web page. We recommend that you do not modify it.-->
        <script>
            var __adobewebfontsappname__ = "dreamweaver"
        </script>
        <script src="http://use.edgefonts.net/montserrat:n4:default;source-sans-pro:n2:default.js" type="text/javascript"></script>
    
    </head>
    
    <body onload="load();">
        <!-- Header content -->
        <header>
            <div class="profileLogo">
                <!-- Profile logo. Add a img tag in place of <span>. -->
                <p class="logoPlaceholder">
                    <!-- <img src="logoImage.png" alt="sample logo"> --><span>Name :</span></p>
                <p class="logoPlaceholder">
                    <!-- <img src="logoImage.png" alt="sample logo"> --><span>Attempts :</span></p>
                <p class="logoPlaceholder">
                    <!-- <img src="logoImage.png" alt="sample logo"> --><span>Score :</span></p>
            </div>
            <section>
                <div class="wrapper">
                    <div class="container">
                        <div class="canvas-container">
    
                            <canvas id="main" width="500px" height="250px"></canvas>
                        </div>
                    </div>
                </div>
            </section>
            <!-- Links to Social network accounts -->
            <aside class="socialNetworkNavBar">
                <div id="left" style="margin-right: 50px;" class="socialNetworkNav">
                    <!-- Add a Anchor tag with nested img tag here -->
                    <input type="image" src="http://via.placeholder.com/75x75">
    
                </div>
                <div id="down" style="margin-right: 50px;" class="socialNetworkNav">
                    <!-- Add a Anchor tag with nested img tag here -->
                    <input type="image" src="http://via.placeholder.com/75x75"> </div>
                <div id="right" style="margin-right: 50px;" class="socialNetworkNav">
                    <!-- Add a Anchor tag with nested img tag here -->
                    <input type="image" src="http://via.placeholder.com/75x75">
                </div>
    
            </aside>
        </header>
        <!-- content -->
    
    </body>
    
    </html>

    抱歉没有给出解释

    【讨论】:

    • 不费吹灰之力,我会尝试阅读代码并找出行为。谢谢朋友!
    • 只是一个简单的问题,如果我想重置位置。我是否应该像下面这样创建一个新功能:function reset(){ imgLoaded = true; context.drawImage(img,250,0); } 这样是否可取,我想在触发输入下降后 10 秒触发它。所以我会使用setTimeout(reset,3000)?
    • 是的。你应该使用function reset() { if (!imgLoaded) return; context.clearRect(0, 0, context.canvas.width, context.canvas.height); context.drawImage(img, 250, 0); }
    • 最后一个问题,onclick可以绑定hold功能吗?
    • 是的,可能(但是,这是一个不同的问题)
    猜你喜欢
    • 2014-09-17
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 2017-11-25
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多