<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#kk {
box-shadow: 0 0 10px 0 sandybrown;
}
</style>
</head>
<body>
<canvas id="kk" width="600" height="600"></canvas>
</body>
<script type="text/javascript">
var myCanvas = document.getElementById("kk");
var context = myCanvas.getContext("2d");
var isDown = false;
function Rect(x, y, w, h, color) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.color = color;
}
Rect.prototype.draw = function() {
context.fillStyle = this.color;
context.fillRect(this.x, this.y, this.w, this.h);
}
var one = new Rect(100, 100, 50, 50, "blue");
var two = new Rect(300, 300, 50, 50, "red");
one.draw();
two.draw();
myCanvas.onmousedown = function(e) {
var x = e.offsetX;
var y = e.offsetY;
if(x > one.x && x < one.x + one.w && y > one.y && y < one.y + one.h) {
isDown = true;
}
}
document.onmouseup = function() {
isDown = false;
}
myCanvas.onmousemove = function(ev) {
if(!isDown) {
return;
}
var x = ev.offsetX;
var y = ev.offsetY;
one.x = x - one.w / 2;
one.y = y - one.h / 2;
context.clearRect(0, 0, myCanvas.width, myCanvas.height);
one.draw();
two.draw();
if(one.x + one.w > two.x && one.x < two.x + two.w && one.y + one.h > two.y && one.y < two.y + two.h) {
console.log("hha");
}
}
</script>
</html>
canvas交互
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- context.drawImage(image,dx,dy); d指destination,s指source co...
- 一、canvas实现弹幕性能较好 从技术实现成本角度讲,要实现弹幕效果,最简单的方法就是DOM+CSS3控制,如果...
- 1、VRTK_InteractableObject:可交互对象 (1)定义:可交互对象脚本被添加到需要用(如控制器...