7.HTML5 高级Canvas技术-2
2013-12-31 04:35
260 查看
<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技术-基本动画
- 7.HTML5 高级Canvas技术-1
- 未来web浏览技术提前体验:10个会让你惊叹不已的HTML5画布(canvas)技术应用演示
- Html5 Canvas核心技术(图形,动画,游戏开发)--基础知识
- 【HTML5】HTML5 高级程序设计 学习笔记2 canvas
- [Web Chart系列之一]Web端图形绘制SVG,VML, HTML5 Canvas 技术比较
- HTML5 Canvas核心技术—图形、动画与游戏开发.pdf7
- 【猪猪-前端】微信打飞机高质量Demo,学习HTML5+Canvas技术编写,下载即可使用,注释齐全。
- HTML5 Canvas实践(二)Canvas 高级功能
- [Web Chart系列之一]Web端图形绘制SVG,VML, HTML5 Canvas 技术比较
- HTML5 Canvas核心技术 图形、动画与游戏开发学习总结
- Html5系列(二十三) canvas高级贝塞尔曲线运动动画
- HTML5 Canvas核心技术—图形、动画与游戏开发.pdf4
- HTML5 Canvas八大核心技术及其API用法
- 【Fanvas技术解密】HTML5 canvas实现脏区重绘
- HTML5 Canvas核心技术—图形、动画与游戏开发.pdf5
- HTML5 CANVAS 高级
- HTML5 Canvas核心技术:图形.动画与游戏开发
- Html5之高级-6 HTML5 Canvas绘图(Canvas概述、Canvas绘图、处理 Canvas 中的图像)
- HTML5之图形绘制技术(Canvas Vs SVG)