<!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交互
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- context.drawImage(image,dx,dy); d指destination,s指source co...
- 一、canvas實(shí)現(xiàn)彈幕性能較好 從技術(shù)實(shí)現(xiàn)成本角度講,要實(shí)現(xiàn)彈幕效果,最簡(jiǎn)單的方法就是DOM+CSS3控制,如果...
- 解決方案:用clearRect刷新canvas。
- 1、VRTK_InteractableObject:可交互對(duì)象 (1)定義:可交互對(duì)象腳本被添加到需要用(如控制器...
- 不知是從什么時(shí)候起,我不再愿意有事沒(méi)事地往外婆家跑。如果可以的話,除非過(guò)年過(guò)節(jié),我不愿被任何一個(gè)借口所束縛捆綁到那...