canvas教程

xueshijun666的专栏

字号+ 作者:H5之家 来源:H5之家 2015-10-20 13:47 我要评论( )

JIRA作为Atlassian公司的明星产品已被广泛熟知,并被誉为全球最潮的项目管理软件。JIRA完美的用户体验 、可定制的工作流 、灵活的敏捷计划,每天都可以让你感受

7.HTML5 高级Canvas技术-2

分类: HTML5

<body> <script> var circles = []; var canvas; var context; var selectedCircle; window.onload = function() { canvas = document.getElementById("drawingCanvas"); context = canvas.getContext("2d"); canvas.onmousedown = canvasClick; canvas.onmouseup = stopDragging; canvas.onmouseout = stopDragging; canvas.onmousemove = dragCircle; }; function Circle(x, y, radius, color) { this.x = x; this.y = y; this.radius = radius; this.color = color; this.isSelected = false; } function addRandomCircle() { var radius = randomFromTo(10, 60); 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(circle); refreshCanvas(); } function clearCanvas() { circles = []; refreshCanvas(); } function refreshCanvas(){ context.clearRect(0, 0, canvas.width, canvas.height); for(var i=0; i<circles.length; i++) { drawCircles(circles[i]); } } function drawCircles(circle) { context.globalAlpha = 0.85; context.beginPath(); context.arc(circle.x, circle.y, circle.radius, 0, Math.PI*2); context.fillStyle = circle.color; context.strokeStyle = "black"; if (circle.isSelected) { context.lineWidth = 5; } else { context.lineWidth = 1; } context.fill(); context.stroke(); } function canvasClick(e) { var clickX = e.pageX - canvas.offsetLeft; var clickY = e.pageY - canvas.offsetTop; for(var i=circles.length-1; i>=0; i--) { var circle = circles[i]; var distanceFromCenter = Math.sqrt(Math.pow(circle.x - clickX, 2) + Math.pow(circle.y - clickY, 2)) if (distanceFromCenter <= circle.radius) { if (selectedCircle != null) selectedCircle.isSelected = false; selectedCircle = circle; circle.isSelected = true; isDragging = true; refreshCanvas(); return; } } } var isDragging = false; function stopDragging() { isDragging = false; } function dragCircle(e) { if (isDragging == true) { if (selectedCircle != null) { var x = e.pageX - canvas.offsetLeft; var y = e.pageY - canvas.offsetTop; selectedCircle.x = x; selectedCircle.y = y; refreshCanvas(); } } } function randomFromTo(from, to) { return Math.floor(Math.random() * (to - from + 1) + from); } </script> <canvas id="drawingCanvas" width="600px" height="600px"></canvas> <div> <button onclick="addRandomCircle()">Add Circle</button> <button onclick="clearCanvas()">Clear Canvas</button> </div> </body>

版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 上一篇7.HTML5 高级Canvas技术-1
  • 下一篇7.HTML5 高级Canvas技术-基本动画
  • 顶 0 踩 0

     

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • 周公(周金桥)的专栏

      周公(周金桥)的专栏

      2016-12-05 18:02

    • 黄志超的专栏

      黄志超的专栏

      2016-07-13 10:00

    • LiuP的专栏

      LiuP的专栏

      2016-06-11 17:00

    • u013591052的专栏

      u013591052的专栏

      2016-01-16 12:35

    网友点评
    n