Javascript HTML使用onclick移动画布对象
是否可以附加onclick事件以向左、向下和向右移动对象Javascript HTML使用onclick移动画布对象,javascript,jquery,html,html5-canvas,Javascript,Jquery,Html,Html5 Canvas,是否可以附加onclick事件以向左、向下和向右移动对象img 按钮是占位符,顺序是左下右 我面临两个问题:- 1.我的宽度是500px,即使我在250px处绘制它。它不居中。 2.我尝试了多种方法将onclick函数添加到-/+的y轴和x轴 我甚至试着适应这个代码: 但也没有运气。 我遗漏了什么吗?我在谷歌上查过的大多数是keyevent处理程序 JSBIN: CSS: HTML: 关于Adobe Dreamweaver CC提供的页面模板 var\uuuu adobebfontsapp
img
按钮是占位符,顺序是左下右
我面临两个问题:-1.我的宽度是
500px
,即使我在250px
处绘制它。它不居中。2.我尝试了多种方法将onclick函数添加到
-/+
的y轴
和x轴
我甚至试着适应这个代码:但也没有运气。 我遗漏了什么吗?我在谷歌上查过的大多数是keyevent处理程序 JSBIN: CSS: HTML:
关于Adobe Dreamweaver CC提供的页面模板
var\uuuu adobebfontsappname=“dreamweaver”
函数加载(){
var context=document.getElementById('main').getContext(“2d”);
var img=新图像();
img.src=”http://via.placeholder.com/75x150";
img.onload=函数(){
背景图像(img,250,0);
}
}
名称:
尝试:
得分:
是强>
这是绝对可能的。以下是你如何做到这一点的
函数加载(){
var context=document.getElementById('main').getContext(“2d”);
var left=document.getElementById('left');
var down=document.getElementById('down');
var right=document.getElementById('right');
var imgLoaded=假;
var-posX=250;
var-posY=0;
var-snap=10;
var img=新图像();
img.onload=函数(){
imgLoaded=真;
背景图像(img、posX、posY);
};
img.src=”http://via.placeholder.com/75x150";
//左键单击
left.onclick=函数(){
如果(!imgLoaded)返回;
posX=posX-捕捉;
clearRect(0,0,context.canvas.width,context.canvas.height);
背景图像(img、posX、posY);
};
//向下按钮单击
down.onclick=函数(){
如果(!imgLoaded)返回;
posY=posY+snap;
clearRect(0,0,context.canvas.width,context.canvas.height);
背景图像(img、posX、posY);
};
//右键单击
right.onclick=function(){
如果(!imgLoaded)返回;
posX=posX+捕捉;
clearRect(0,0,context.canvas.width,context.canvas.height);
背景图像(img、posX、posY);
};
}
@charset“utf-8”;
/*全局样式*/
#主要{
宽度:100%;
背景色:红色;
}
身体{
左边距:自动;
右边距:自动;
宽度:92%;
最大宽度:960像素;
左:2.275%;
右侧填充:2.275%;
}
/*标题*/
标题{
文本对齐:居中;
显示:块;
}
header.profileLogo.logoPlaceholder{
背景色:白色;
颜色:黑色;
宽度:300px;
文本对齐:居中;
}
header.logo占位符span{
宽度:180px;
高度:22px;
字体系列:“蒙特塞拉特”,无衬线;
颜色:黑色;
字体大小:30px;
字号:700;
线高:53px;
}
页眉.logo占位符2 span{
宽度:180px;
高度:22px;
字体系列:“蒙特塞拉特”,无衬线;
颜色:黑色;
字体大小:30px;
字号:700;
线高:53px;
}
标题.简介照片{
背景色:rgba(237,237,237,1.00);
宽度:259px;
边界半径:50%;
高度:259px;
明确:两者皆有;
}
header.profileHeader h1{
字体系列:“蒙特塞拉特”,无衬线;
颜色:rgba(146、146、146、1.00);
字体大小:30px;
字号:700;
线高:24px;
}
header.profileHeader h3{
字体系列:无衬线;
颜色:rgba(146、146、146、1.00);
字体大小:16px;
字号:700;
线高:24px;
}
人力资源{
背景色:rgba(208、207、207、1.00);
高度:1px;
}
header.profilep头文件{
字体系列:无衬线;
颜色:rgba(146、146、146、1.00);
字体大小:14px;
字体大小:400;
线高:1.8;
溢出y:自动;
}
header.socialNetworkNavBar{
文本对齐:居中;
显示:块;
边缘顶部:60像素;
明确:两者皆有;
}
header.socialNetworkNavBar.socialNetworkNav{
边界半径:50%;
光标:指针;
}
header.socialNetworkNavBar.socialNetworkNav img:hover{
不透明度:0.5;
}
/*移动设备的媒体查询*/
@仅介质屏幕和(最小宽度:285px)和(最大宽度:480px){
/*标题*/
#主要{
宽度:100%;
}
标题{
宽度:100%;
浮动:左;
}
.包装纸{
显示:内联块;
宽度:100%;
身高:100%;
}
header.profileLogo.logoPlaceholder{
显示:内联块;
文本对齐:左对齐;
}
标题.简介照片{
显示:内联块;
}
header.profileHeader{
文本对齐:居中;
利润率最高:10%;
}
header.profilep头文件{
文本对齐:对齐;
}
header.socialNetworkNavBar{
文本对齐:居中;
显示:块;
边缘顶部:60像素;
明确:两者皆有;
利润底部:15%;
}
header.socialNetworkNavBar.socialNetworkNav{
宽度:60px;
高度:60px;
显示:内联块;
右边距:23px;
}
}
/*平板电脑的媒体查询*/
@仅介质屏幕和(最小宽度:481px)和(最大宽度:1024px){
/*标题*/
标题{
宽度:100%;
浮动:无;
}
header.profileLogo.logoPlaceholder{
显示:内联块;
字体系列:“蒙特塞拉特”,无衬线;
}
标题.简介照片{
浮动:左;
明确:两者皆有;
保证金权利:3%;
利润率最高:5%;
}
header.profileHeader{
文本对齐:左对齐;
利润率最高:7%;
}
header.socialNetworkNavBar.socialNetworkNav{
@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>
<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>