使用html5 canvas 绘制Android机器人
来源:互联网 发布:淘宝助手怎么上架宝贝 编辑:程序博客网 时间:2024/06/09 13:58
练习使用Html5 canvas 绘制Android机器人。
要点:当对画布进行变换的时候,需要使用 save() 方法 和 restore() 方法,它们应该成对使用,且 restore() 方法不能比 save() 方法多。我们只需要每次在画布变换前使用 save() 方法保存其初始状态,之后使用 restore() 方法恢复到上一次保存的状态即可。
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style type="text/css"> * { margin: 0; padding: 0; } #canvas { border: 1px solid #000; margin: 0 auto; display: block; } #show { position: fixed; top: 10px; left: 10px; } </style> </head> <body> <canvas width="1000" height="1024" id="canvas" onmousemove="go(event)" onmouseout="out()"> 您的浏览器不支持canvas,请升级你的浏览器! </canvas> <div id="show"></div> </body> <script type="text/javascript"> //获得坐标,便于查找 var show=document.getElementById("show"); document.getElementById("show").innerHTML='ds' function go(e){ var x=e.clientX; var y=e.clientY; document.getElementById("show").innerHTML=x+" "+y; } function out(){ show.innerHTML=""; } //获得canvas对象 var oCanvas = document.getElementById('canvas'); var ctx = oCanvas.getContext('2d'); //头 ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.arc(500, 300, 137, Math.PI * (-18 / 180), Math.PI * (-175 / 180), 1); ctx.fill(); ctx.closePath(); //身子 ctx.beginPath(); ctx.restore(); ctx.fillStyle = '#99FE00'; ctx.save(); ctx.rotate(-7 * Math.PI / 180); ctx.fillRect(325, 350, 268, 200); ctx.closePath(); //左眼 ctx.restore(); ctx.beginPath(); ctx.fillStyle = '#fff'; ctx.arc(430, 230, 16, 0, Math.PI * 2, 1); ctx.fill(); ctx.closePath(); //右眼 ctx.beginPath(); ctx.fillStyle = '#fff'; ctx.arc(535, 220, 16, 0, Math.PI * 2, 1); ctx.fill(); ctx.closePath(); //左腿 ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.fillRect(460, 470, 48, 100); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.moveTo(483, 562); ctx.arc(484, 562, 25, Math.PI * (15 / 180), Math.PI * (165 / 180), 0); ctx.fill(); ctx.closePath(); //右腿 ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.fillRect(550, 450, 48, 120); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.moveTo(573, 562); ctx.arc(574, 562, 25, Math.PI * (15 / 180), Math.PI * (165 / 180), 0); ctx.fill(); ctx.closePath(); //左手 ctx.save(); ctx.rotate(55 * Math.PI / 180); ctx.translate(145, -430); ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.moveTo(340, 355); ctx.arc(340, 355, 24, 0, Math.PI, 1); ctx.fill(); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.fillRect(316, 355, 48, 100); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.moveTo(340, 450); ctx.arc(340, 450, 24, 0, Math.PI, 0); ctx.fill(); ctx.closePath(); //右手 ctx.restore(); ctx.save(); ctx.rotate(30 * Math.PI / 180); ctx.translate(370, -545); ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.moveTo(340, 355); ctx.arc(340, 355, 24, 0, Math.PI, 1); ctx.fill(); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.fillRect(316, 355, 48, 100); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.moveTo(340, 450); ctx.arc(340, 450, 24, 0, Math.PI, 0); ctx.fill(); ctx.closePath(); //左触角 ctx.restore(); ctx.beginPath(); ctx.strokeStyle = "#99FE00"; ctx.moveTo(430,190); ctx.lineTo(390,140); ctx.lineWidth = 10; ctx.stroke(); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.arc(390, 140, 5, 0, Math.PI * 2, 1); ctx.fill(); ctx.closePath(); //左触角 ctx.beginPath(); ctx.strokeStyle = "#99FE00"; ctx.moveTo(530,180); ctx.lineTo(550,120); ctx.lineWidth = 10; ctx.stroke(); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = '#99FE00'; ctx.arc(550, 120, 5, 0, Math.PI * 2, 1); ctx.fill(); ctx.closePath(); //滑板 ctx.beginPath(); ctx.fillStyle = '#000'; ctx.fillRect(400, 584, 300, 40); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = '#000'; ctx.arc(400, 604, 20, 0, Math.PI * 2, 1); ctx.fill(); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = '#000'; ctx.arc(700, 604, 20, 0, Math.PI * 2, 1); ctx.fill(); ctx.closePath(); //滑板轮 ctx.beginPath(); ctx.fillStyle = '#000'; ctx.arc(450, 658, 20, 0, Math.PI * 2, 1); ctx.fill(); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = '#000'; ctx.arc(640, 658, 20, 0, Math.PI * 2, 1); ctx.fill(); ctx.closePath(); //Android渐变文字 ctx.beginPath(); ctx.font = '60px Arial'; var grd=ctx.createLinearGradient(400, 0, 600, 0); grd.addColorStop(0,"black"); grd.addColorStop(0.3,"magenta"); grd.addColorStop(0.5,"blue"); grd.addColorStop(0.6,"green"); grd.addColorStop(0.8,"yellow"); grd.addColorStop(1,"red"); ctx.fillStyle=grd; ctx.fillText('Android', 400, 750); ctx.closePath(); </script> </html>
下面是代码效果:
0 0
- 使用html5 canvas 绘制Android机器人
- 使用html5 canvas绘制图片
- HTML5使用canvas绘制图形
- HTML5使用canvas绘制图形
- 使用 HTML5 canvas 绘制精美的图形
- HTML5开发实例-使用canvas绘制曲线
- 使用 HTML5 canvas 绘制精美的图形
- 使用HTML5中的canvas绘制灰太狼图像
- 使用HTML5 Canvas绘制一个时钟
- HTML5中使用canvas绘制复杂图形
- HTML5中使用canvas绘制矩形
- HTML5中使用canvas绘制圆形
- angular 使用html5 canvas绘制圆组建
- HTML5之Canvas绘图——使用Canvas绘制图形
- 【Html5】canvas绘制圆形
- html5 canvas 绘制钟表
- HTML5 Canvas: 绘制矩形
- HTML5 canvas 绘制圆形
- iOS开发: 多线程
- c语言字符输出格式化
- 钱究竟是什么东西(一)?
- 详谈gson解析
- C++那些细节--const与函数
- 使用html5 canvas 绘制Android机器人
- 2015 ACM/ICPC Asia Regional Shanghai Online A Sweet Journey(简单题)
- c++如何处理多维数组
- 【Java】Java线程的概念
- 论自动化测试是否可以完全取代手工测试?
- struts1工作原理
- Linux网络虚拟化与MLPPP多链路
- android MSM8974 上DeviceTree简介
- 记录——《C Primer Plus (第五版)》第七章编程练习第八题