39,087
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html>
<html>
<head>
<title> Canvas Demo</title>
<!--[if It IE]>
<script type="text/javascript" src="./excanvas.js"></script>
<![endif]-->
<script>
//初始化
window.onload=function() {
var canvas = document.getElementById('myCanvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
ctx.fillStyle='blue';
// -----------------------------
ctx.beginPath();
ctx.rect(10,10,50,50);
ctx.fill();
ctx.beginPath();
ctx.rect(70,10,50,50);
ctx.fill();
ctx.beginPath();
ctx.rect(130,10,50,50);
ctx.fill();
ctx.beginPath();
ctx.rect(190,10,50,50);
ctx.fill();
ctx.beginPath();
ctx.rect(250,10,50,50);
ctx.fill();
ctx.beginPath();
ctx.rect(310,10,50,50);
ctx.fill();
//----------------------------------------
//添加事件响应
canvas.addEventListener('click', function(e){
p = getEventPosition(e);
reDraw(p,ctx);
}, false);
}
}
//得到点击的坐标
function getEventPosition(ev){
var x, y;
if (ev.layerX || ev.layerX == 0) {
x = ev.layerX;
y = ev.layerY;
}else if (ev.offsetX || ev.offsetX == 0) { // Opera
x = ev.offsetX;
y = ev.offsetY;
}
return {x: x, y: y};
}
//重绘
function reDraw(p,ctx){
arr = [
{x:10, y:10, width:50, height:50},
{x:70, y:10, width:50, height:50},
{x:130, y:10, width:50, height:50},
{x:190, y:10, width:50, height:50},
{x:250, y:10, width:50, height:50},
{x:310, y:10, width:50, height:50},
]
var whichObject = [];
for(var i=0; i < arr.length; i++){
ctx.fillStyle='blue';
ctx.beginPath();
ctx.rect(arr[i].x,arr[i].y, arr[i].width,arr[i].height);
ctx.fill();
if(p && ctx.isPointInPath(p.x, p.y)){
whichObject.push(i);
ctx.fillStyle='red';
ctx.beginPath();
ctx.rect(arr[i].x,arr[i].y, arr[i].width,arr[i].height);
ctx.fill();
}
}
//显示点击了哪个部分
// alert("click:" + whichObject[0]);
}
</script>
</head>
<body>
<div>
<canvas id="myCanvas" width="600" height="600"></canvas>
<br /><br />
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> Canvas Demo</title>
<!--[if It IE]>
<script type="text/javascript" src="./excanvas.js"></script>
<![endif]-->
<script>
//初始化
window.onload=function() {
var canvas = document.getElementById('myCanvas');
if (canvas.getContext){
var ctx = canvas.getContext('2d');
ctx.fillStyle='blue';
// -----------------------------
ctx.beginPath();
ctx.rect(10,10,50,50);
ctx.fill();
ctx.beginPath();
ctx.rect(70,10,50,50);
ctx.fill();
ctx.beginPath();
ctx.rect(130,10,50,50);
ctx.fill();
ctx.beginPath();
ctx.rect(190,10,50,50);
ctx.fill();
ctx.beginPath();
ctx.rect(250,10,50,50);
ctx.fill();
ctx.beginPath();
ctx.rect(310,10,50,50);
ctx.fill();
//----------------------------------------
//添加事件响应
canvas.addEventListener('click', function(e){
p = getEventPosition(e);
reDraw(p,ctx);
}, false);
}
}
//得到点击的坐标
function getEventPosition(ev){
var x, y;
if (ev.layerX || ev.layerX == 0) {
x = ev.layerX;
y = ev.layerY;
}else if (ev.offsetX || ev.offsetX == 0) { // Opera
x = ev.offsetX;
y = ev.offsetY;
}
return {x: x, y: y};
}
var arr = [
{x:10, y:10, width:50, height:50},
{x:70, y:10, width:50, height:50},
{x:130, y:10, width:50, height:50},
{x:190, y:10, width:50, height:50},
{x:250, y:10, width:50, height:50},
{x:310, y:10, width:50, height:50}
];
//重绘
function reDraw(p,ctx){
var whichObject = [];
for(var i=0; i < arr.length; i++){
if(p && (arr[i].x + arr[i].width) >= p.x && p.x >= arr[i].x
&& (arr[i].y + arr[i].height) >= p.y && p.y >= arr[i].y){
whichObject.push(i);
if(arr[i].selected){
ctx.fillStyle='blue';
arr[i].selected = false;
} else {
ctx.fillStyle='red';
arr[i].selected = true;
}
ctx.beginPath();
ctx.rect(arr[i].x,arr[i].y, arr[i].width,arr[i].height);
ctx.fill();
break;
}
}
}
</script>
</head>
<body>
<div>
<canvas id="myCanvas" width="600" height="600" style="background:#8D8D8D"></canvas>
<br /><br />
</div>
</body>
</html>