canvas 记录绘画对象实现交互能力 简单碰撞检测
2014-12-24 17:52
507 查看
<!DOCTYPE HTML> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1 user-scalable=no"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> <title>Interactive Canvas demo</title> <style type="text/css"> #myCanvas{border: 1px solid;} </style> </head> <body> <div class="container"> <p>Interactive Canvas demo from "The missing manual HTML5" --Matthew MacDonald</p> <canvas id="myCanvas" width="600" height="400"></canvas><br> <button onclick="addRadomCircle()">add circle</button> <div> <ul> <li>draw circles randomly in canvas</li> <li>able to select drawn circle</li> <li>recreate the whole canvas once new circle added or circle selected</li> <li>implement simple collision detection</li> <li>random integer function</li> <li>class and object</li> <li>manuplate array</li> </ul> </div> </div> <script type="text/javascript"> //create global variables var canvas; var context; var previousSelectedCircle; //window.onload: wait until all elements loaded including imgs //jquery $(document).ready(function(){}): wait until all dom structure loaded window.onload = function(){ canvas=document.getElementById("myCanvas"); context=canvas.getContext("2d"); context.strokeStyle = "black"; canvas.onmousedown = canvasClick; //bind event to element } // create a class function Circle(x,y,radius,color){ this.x = x; this.y = y; this.radius = radius; this.color = color; this.isSelected = false; } // create an array to store all circles drawn var circles = []; // add a circle using random parameters function addRadomCircle(){ var radius = randomFromTo(10,80); var x = randomFromTo(0,canvas.width); var y = randomFromTo(0,canvas.height); var colors = ["green", "blue","red", "yellow", "magenta", "orange", "brown", "purple", "pink"]; var color = colors[randomFromTo(0,8)]; var circle = new Circle(x,y,radius,color); circles.push(circle); drawCircles(); // draw all circles in circles array } // refresh the whole canvs and draw all circles function drawCircles(){ // clear the canvas context.clearRect(0,0,canvas.width,canvas.height); // set transparency context.globalAlpha = 0.85; // iterate circles array for(var i=0; i<circles.length; i++){ var circle = circles[i]; context.beginPath(); context.arc(circle.x,circle.y,circle.radius,0,Math.PI*2); context.fillStyle = circle.color; // add border if selected if(circle.isSelected){ context.lineWidth = 5; context.fill(); context.stroke(); } else { context.fill(); } } } // check mouse click on any circle or empty area function canvasClick(e){ var clickX = e.pageX - canvas.offsetLeft; // offsetleft: horizontal offset from first parent element which has a css position var clickY = e.pageY - canvas.offsetTop; // iterate circles in a reverse order for (var i=circles.length-1; i>=0; i--){ var circle = circles[i]; // caculate distance between click point and center of circle using Pythagorean theorem var distance = Math.sqrt(Math.pow(circle.x-clickX,2)+Math.pow(circle.y-clickY,2)); if (distance <= circle.radius){ // inside circle // reset isSelected attribute state if(previousSelectedCircle != null) previousSelectedCircle.isSelected = false; previousSelectedCircle = circle; circle.isSelected = true; // refresh the canvas drawCircles(); return;// exit iteration } } } // create a random integer with closed interval ([a,b]) function randomFromTo(from, to) { return Math.floor(Math.random() * (to - from + 1) + from); } </script> </body> </html> <!-- ref: http://www.prosetech.com/html5/Chapter%2009/InteractiveCircles.html -->
相关文章推荐
- HTML5 Canvas 碰撞检测的简单实现
- [Unity3D]在Unity3D中实现简单的碰撞检测
- 在Unity3D中实现简单的碰撞检测 及可能出现的错误
- 在Unity3D中实现简单的碰撞检测
- 在Unity3D中实现简单的碰撞检测
- Unity实现简单的碰撞检测
- Unity实现简单的碰撞检测
- Java编程实现游戏中的简单碰撞检测功能示例
- Unity实现简单的碰撞检测
- 从头认识Spring-3.4 简单的AOP日志实现-扩展增加检查订单功能,以便记录并检测输入的参数
- 从头认识Spring-3.8 简单的AOP日志实现(注解版)-扩展增加检查订单功能,以便记录并检测输入的参数
- OpenGL实现碰撞检测与模拟重力效果(简单的物理系统)
- android 3D 游戏实现之简单碰撞检测
- [Unity3D]在Unity3D中实现简单的碰撞检测
- Java Web中的入侵检测及简单实现
- Java Web中的入侵检测及简单实现
- Java Web中的入侵检测及简单实现
- Java Web中的入侵检测及简单实现
- Java Web中的入侵检测及简单实现
- Java Web中的入侵检测及简单实现