HTML5两个画布如何弄,使用HTML5将两个画布方块设置到画布的中心(Set two canvas squares to the center of a canvas with HTML5)...

I have two boxes positioned on a canvas that I am trying to center. You can view this on JS fiddle: http://jsfiddle.net/FVU47/5/

My canvas has 1000 height and 1000 width as follows:

I am then attempting to center the two boxes with the following code, which would place either box1 or box2 in the center of the canvas, depending on whether I click on "Go to Box 1" or "Go to Box 2" (see the bottom of the JSFiddle Result quadrant:

$(document).ready(function(){

$("#box1click").click(function(){

if (rect1.x <= 500) {

positionWidthSet = Math.abs(rect1.x - canvas.width/2) + rect1.x;

}

else{

positionWidthSet = Math.abs(Math.abs(rect1.x - canvas.width/2) + rect1.x)

}

if (rect1.y >= 500) {

positionHeightSet = Math.abs(rect1.y -canvas.height/2);

}

else{

positionHeightSet = Math.abs(Math.abs(rect1.y - canvas.height/2) + rect1.y);

}

positionCanvasContext(positionWidthSet,positionHeightSet);

});

});

$(document).ready(function(){

$("#box2click").click(function(){

if (rect2.x <= 500) {

positionWidthSet = Math.abs(rect2.x - canvas.width/2) + rect2.x;

}

else{

positionWidthSet = Math.abs(Math.abs(rect2.x - canvas.width/2) + rect2.x)

}

if (rect2.y >= 500) {

positionHeightSet = Math.abs(rect2.y -canvas.height/2);

}

else{

positionHeightSet = Math.abs(Math.abs(rect2.y - canvas.height/2) + rect2.y);

}

positionCanvasContext(positionWidthSet,positionHeightSet);

});

});

Currently, clicking on either "Go to Box 1" or "Go to Box 2" does not center the canvas around either Box 1 or Box 2, even though experimenting with my formulas in the console would seem to indicate otherwise.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值