制作一个画画板,有清屏有橡皮擦有画笔可以换颜色

style样式

  1. <head>
  2. <meta charset="UTF-8">
  3. <title>画画板</title>
  4. <style>
  5. body{
  6. background-color:#ccc;
  7. }
  8. .control-bar{
  9. vertical-align:top;
  10. display:inline-block;
  11. }
  12. </style>
  13. </head>

html结构

  1. <canvas id="myCanvas"></canvas>
  2. <div class="control-bar">
  3. <button id="clearBtn">清屏</button>
  4. <button id="penBtn">画笔</button>
  5. <input type="color" id="colorBtn">
  6. <button id="eraserBtn">橡皮擦</button>
  7. </div>

script

  1. <script>
  2. (function(){
  3. var w=800;//画画板的宽度
  4. var h=600; //画画板的高度
  5. //获取相关元素
  6. var canvas=document.getElementById("myCanvas");
  7. var penBtn=document.getElementById("penBtn");
  8. var colorBtn=document.getElementById("colorBtn");
  9. var eraserBtn=document.getElementById("eraserBtn");
  10. var clearBtn=document.getElementById("clearBtn");
  11. //画布大小设置
  12. canvas.width=w;
  13. canvas.height=h;
  14. canvas.style.backgroundColor="#fff";
  15. //获取绘图环境
  16. var ctx=canvas.getContext("2d");
  17. //鼠标按下事件
  18. canvas.onmousedown=function(ev){
  19. //求鼠标此时坐标
  20. var x=ev.clientX-canvas.getBoundingClientRect().left;//getBoundingClientRect用于获取某个元素相对于视窗的位置集合
  21. var y=ev.clientY-canvas.getBoundingClientRect().top+32;//32画笔/橡皮擦的宽度用于准确的定位
  22. //开启路径 绘制起点
  23. ctx.beginPath();
  24. ctx.moveTo(x,y);
  25. //鼠标移动
  26. canvas.onmousemove=function(ev){
  27. //求鼠标此时坐标
  28. var x=ev.clientX-canvas.getBoundingClientRect().left;
  29. var y=ev.clientY-canvas.getBoundingClientRect().top+32;
  30. ctx.lineTo(x,y);
  31. //绘制
  32. ctx.stroke();
  33. }
  34. }
  35. //鼠标抬起
  36. canvas.onmouseup=function(){
  37. this.onmousemove=function(){}
  38. }
  39. setPen();//默认画笔
  40. //点击橡皮擦
  41. eraserBtn.onclick=setEraser;
  42. //点击画笔
  43. penBtn.onclick=setPen;
  44. //点击颜色选择
  45. colorBtn.onchange=setPen;
  46. //点击清屏
  47. clearBtn.onclick=function(){
  48. //ctx.clearRect(0,0,w,h)//和下面两种变法任选其一
  49. canvas.width=canvas.width;
  50. //重新设置画布的宽度,可以清除屏幕
  51. setPen();
  52. }
  53. //设置为画笔的函数
  54. function setPen(){
  55. ctx.lineWidth=4;
  56. ctx.strokeStyle=colorBtn.value;
  57. document.body.style.cursor="url('./images/pen2.ico'),auto";
  58. }
  59. //设置为橡皮擦的函数
  60. function setEraser(){
  61. ctx.lineWidth=20;
  62. ctx.strokeStyle="#fff";
  63. document.body.style.cursor="url('./images/eraser2.ico'),auto";
  64. }
  65. })()
  66. </script>

制作五角星或者多边形

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>旋转--</title>
  6. <style>
  7. body{
  8. background:#ccc;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <canvas id="myCanvas"></canvas>
  14. <script>
  15. var canvas=document.getElementById("myCanvas");
  16. canvas.width=800;
  17. canvas.height=600;
  18. canvas.style.backgroundColor="#fff";
  19. var ctx=canvas.getContext("2d");
  20. drawStar(ctx,400,300,40,100);
  21. drawStar(ctx,150,150,80,120,"yellow","red",5,12);
  22. //声明绘制 五角星的函数
  23. function drawStar(ctx,cx,cy,innerRadius,outerRadius,fill="transparent",stroke="#000",strokeWidth=2,numPoints=5){
  24. var angle=Math.PI*2/numPoints;//每个角之间的弧度间隔
  25. var startAngle=-Math.PI/2;//开始点的弧度
  26. ctx.beginPath();
  27. for(let i=0;i<numPoints;i++){
  28. let x1=cx+outerRadius*Math.cos(startAngle+angle*i);
  29. let y1=cy+outerRadius*Math.sin(startAngle+angle*i);
  30. ctx.lineTo(x1,y1);
  31. let x2=cx+innerRadius*Math.cos(startAngle+angle*i+angle/2);
  32. let y2=cy+innerRadius*Math.sin(startAngle+angle*i+angle/2);
  33. ctx.lineTo(x2,y2);
  34. }
  35. ctx.closePath();
  36. ctx.fillStyle=fill;
  37. ctx.strokeStyle=stroke;
  38. ctx.lineWidth=strokeWidth;
  39. ctx.stroke();
  40. ctx.fill();
  41. }
  42. </script>
  43. </body>
  44. </html>

canvas制作钟表

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>canvas钟表</title>
  6. <style>
  7. canvas{
  8. position:absolute;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <canvas id="dialCanvas"></canvas>
  14. <canvas id="handCanvas"></canvas>
  15. <script>
  16. (function(){
  17. //定义相关设置
  18. var width=600;
  19. var height=600;
  20. var dialRadius=200;//表盘半径
  21. var cx=width/2;
  22. var cy=height/2;
  23. //获取canvas
  24. var dialCanvas=document.getElementById("dialCanvas");
  25. var handCanvas=document.getElementById("handCanvas");
  26. //设置画布大小
  27. dialCanvas.width=width;
  28. dialCanvas.height=height;
  29. handCanvas.width=width;
  30. handCanvas.height=height;
  31. //获取绘图环境
  32. var dialCtx=dialCanvas.getContext("2d");
  33. var handCtx=handCanvas.getContext("2d");
  34. //绘制表盘
  35. dialCtx.beginPath();
  36. dialCtx.arc(cx,cy,dialRadius,0,2*Math.PI);
  37. dialCtx.lineWidth=10;
  38. dialCtx.stroke();
  39. //绘制小时的刻度
  40. drawScale(dialCtx,cx,cy,dialRadius,dialRadius-20,"#000",10,12);
  41. //绘制分钟的刻度
  42. drawScale(dialCtx,cx,cy,dialRadius,dialRadius-10,"#000",5,60);
  43. runTime();
  44. //定义定时函数
  45. function runTime(){
  46. //获取当前时间
  47. var date=new Date();
  48. var s=date.getSeconds();
  49. var m=date.getMinutes()+s/60;
  50. var h=date.getHours()+m/60;
  51. //清除屏幕
  52. handCtx.clearRect(0,0,width,height);
  53. //绘制秒针
  54. drawHand(handCtx,cx,cy,s/60*Math.PI*2,180,"red",2);
  55. //绘制分针
  56. drawHand(handCtx,cx,cy,m/60*Math.PI*2,150,"#000",5);
  57. //绘制时针
  58. drawHand(handCtx,cx,cy,h/12*Math.PI*2,120,"#000",8);
  59. setTimeout(runTime,1000);
  60. /**
  61. * 绘制指针
  62. * @params object ctx
  63. * @params number cx 表盘圆心坐标
  64. * @params number cy表盘圆心坐标
  65. * @params number angle 旋转的度数(弧度)
  66. * @params number handLength指针长度
  67. * @params string stroke指针颜色
  68. * @params number strokeWidth 指针粗细度
  69. */
  70. function drawHand(ctx,cx,cy,angle,handLength,stroke="#000",strokeWidth=2){
  71. ctx.save();
  72. ctx.translate(cx,cy);
  73. ctx.rotate(angle-Math.PI/2);
  74. ctx.beginPath();
  75. ctx.moveTo(-20,0);
  76. ctx.lineTo(handLength,0);
  77. ctx.strokeStyle=stroke;
  78. ctx.lineWidth=strokeWidth;
  79. ctx.stroke();
  80. ctx.restore();
  81. }
  82. /**
  83. * 绘制刻度的函数
  84. * @param object ctx
  85. * @param number cx 圆心坐标
  86. * @param number cy 圆心坐标
  87. * @param number innerRadius 内半径
  88. * @param number outerRadius 外半径
  89. * @param string stroke 刻度颜色
  90. * @param number strokeWidth 刻度宽度
  91. * @param number numScales 刻度数量
  92. */
  93. }
  94. function drawScale(ctx,cx,cy,innerRadius,outerRadius,stroke="#000",strokeWidth=2,numScales=12){
  95. ctx.beginPath();
  96. var angle=0;
  97. var changeAngle=Math.PI*2/numScales;
  98. for(var i=0;i<numScales;i++){
  99. //外圆上的点
  100. var x1=cx+outerRadius*Math.cos(angle);
  101. var y1=cy+outerRadius*Math.sin(angle);
  102. //内圆上的点
  103. var x2=cx+innerRadius*Math.cos(angle);
  104. var y2=cy+innerRadius*Math.sin(angle);
  105. ctx.moveTo(x1,y1);
  106. ctx.lineTo(x2,y2);
  107. angle+=changeAngle;
  108. }
  109. ctx.strokeStyle=stroke;
  110. ctx.lineWidth=strokeWidth;
  111. ctx.stroke();
  112. }
  113. })()
  114. </script>
  115. </body>
  116. </html>

Konva写钟表

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>konva 钟表</title>
  6. <style>
  7. html{
  8. overflow:hidden;
  9. }
  10. body{
  11. margin:0;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="clock"></div>
  17. <script src="../konva/konva.min.js"></script>
  18. <script>
  19. (function(){
  20. //创建舞台
  21. var stage=new Konva.Stage({
  22. width:window.innerWidth,
  23. height:window.innerHeight,
  24. container:"#clock"
  25. });
  26. //创建表盘图层
  27. var dialLayer=new Konva.Layer({
  28. });
  29. stage.add(dialLayer);
  30. //表盘圆圈
  31. var circle=new Konva.Circle({
  32. x:stage.getWidth()/2,
  33. y:stage.getHeight()/2,
  34. radius:200,
  35. stroke:"#000",
  36. strokeWidth:10
  37. });
  38. dialLayer.add(circle);
  39. //绘制小时刻度
  40. var hourScale=new DialScale({
  41. x:stage.getWidth()/2,
  42. y:stage.getHeight()/2,
  43. outerRadius:200,
  44. innerRadius:180,
  45. strokeWidth:10,
  46. stroke:"#000"
  47. });
  48. dialLayer.add(hourScale);
  49. //绘制分钟的坐标
  50. var minuteScale=new DialScale({
  51. x:stage.getWidth()/2,
  52. y:stage.getHeight()/2,
  53. outerRadius:200,
  54. innerRadius:190,
  55. strokeWidth:6,
  56. stroke:"#000",
  57. numScales:60
  58. });
  59. dialLayer.add(minuteScale);
  60. dialLayer.draw();
  61. //创建指针图层
  62. var handLayer=new Konva.Layer({
  63. x:stage.getWidth()/2,
  64. y:stage.getHeight()/2
  65. });
  66. stage.add(handLayer);
  67. //秒钟
  68. var secondHand=new Konva.Line({
  69. points:[-20,0,180,0],
  70. stroke:"red",
  71. strokeWidth:2
  72. });
  73. handLayer.add(secondHand);
  74. //分针
  75. var minuteHand=new Konva.Line({
  76. points:[-20,0,150,0],
  77. stroke:"#000",
  78. strokeWidth:5
  79. });
  80. handLayer.add(minuteHand);
  81. //时针
  82. hourHand=new Konva.Line({
  83. points:[-20,0,120,0],
  84. stroke:"#000",
  85. strokeWidth:8
  86. });
  87. handLayer.add(hourHand);
  88. //小圆圈
  89. var smallCircle=new Konva.Circle({
  90. x:0,
  91. y:0,
  92. radius:10,
  93. fill:"#000"
  94. });
  95. handLayer.add(smallCircle);
  96. handLayer.draw();
  97. runTime();
  98. function runTime(){
  99. var date=new Date();
  100. var s=date.getSeconds();
  101. var m=date.getMinutes()+s/60;
  102. var h=date.getHours()+m/60;
  103. secondHand.rotation(s/60*360-90);
  104. minuteHand.rotation(m/60*360-90);
  105. hourHand.rotation(h/12*360-90);
  106. handLayer.draw();
  107. setTimeout(runTime,1000);
  108. }
  109. // 绘制刻度的构造函数
  110. function DialScale(options){
  111. options=options ||{};
  112. this.x=options.x ||0;
  113. this.y=options.y || o;
  114. this.innerRadius = options.innerRadius || 0;
  115. this.outerRadius = options.outerRadius ||0;
  116. this.numScales=options.numScales || 12;
  117. this.stroke=options.stroke ||"#000";
  118. this.strokeWidth=options.strokeWidth ||2;
  119. var group=new Konva.Group({
  120. x:this.x,
  121. y:this.y
  122. });
  123. var angle=0;
  124. var changeAngle=Math.PI*2/this.numScales;
  125. for(var i=0;i<this.numScales;i++){
  126. //外圆的点
  127. var x1=Math.cos(angle)*this.outerRadius;
  128. var y1=Math.sin(angle)*this.outerRadius;
  129. //内圆的点
  130. var x2=Math.cos(angle)*this.innerRadius;
  131. var y2=Math.sin(angle)*this.innerRadius;
  132. var line=new Konva.Line({
  133. points:[x1,y1,x2,y2],
  134. stroke:this.stroke,
  135. strokeWidth:this.strokeWidth
  136. });
  137. group.add(line);
  138. angle+=changeAngle;
  139. }
  140. return group;
  141. }
  142. })();
  143. </script>
  144. </body>
  145. </html>

canvas画画板,canvas画五角星,canvas制作钟表、Konva写钟表的更多相关文章

  1. Html5新特性 &lt;canvas&gt;画板画直线

     以下样例为用canvas标签画多条直线 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  2. Android利用canvas画画板

    首先新建一个项目工程,建立文件,如下图所示

  3. canvas游戏小试:画一个按方向键移动的圆点

    canvas游戏小试:画一个按方向键移动的圆点   自己对canvas,但又有一颗做游戏的心TT.然后记录一下对canvas的学习吧,用一个按方向键控制的小圆点来做练习.(编程时用了一些es6的语法) ...

  4. 【分享】用Canvas实现画板功能

    前言 PC端测试:QQ浏览器全屏绘画完成.缩小时内容会被清空,切换背景颜色内容会被重置,其他暂无发现: 手机端测试:微信内置浏览器不通过:Safari 浏览器使用画笔时没固定页面会有抖动效果,使用橡皮 ...

  5. canvas小画板——(2)荧光笔效果

    我们在上一篇文章中讲了如何绘制平滑曲线 canvas小画板——(1)平滑曲线. 透明度实现荧光笔 现在我们需要加另外一种画笔效果,带透明度的荧光笔.那可能会觉得绘制画笔的时候加上透明度就可以了.我们来 ...

  6. canvas小画板——(3)笔锋效果

    画线准备 准备一个canvas <canvas id="canvasId" width="1000" height="800"> ...

  7. canvas实现画板

    canvas实现画板 主要用到知识点: 鼠标事件onmousedown() onmousemove() onmouseup() onmouseleave() 事件委托 canvas的一些方法 如:绘制 ...

  8. canvas简易画板

    代码展示: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  9. 一款基于HTML5 Canvas的画板涂鸦动画

    今天给各网友分享一款基于HTML5 Canvas的画板涂鸦动画.记得之前我们分享过一款HTML5 Canvas画板工具,可以切换不同的笔刷,功能十分强大.本文今天要再来分享一款基于HTML5 Canv ...

随机推荐

  1. Andorid API Package ---> android.app

    包名: android.app                                     Added in API level 1       URL:http://developer. ...

  2. Oracle 获取 某个表的建表SQL

    获取A表的创表SQL select dbms_metadata.get_ddl('TABLE','A') from dual

  3. 【题解】CF#1012 C-Hill

    感觉这题的状态还是比较明显的.设置状态 \(f[i][j][0/1]\) 表示dp到第 \(i\) 个位置,前面(包括这里)已经出现了 \(j\) 个山峰,当前位置是不是山峰即可 dp.这样的状态有一 ...

  4. BZOJ3534:[SDOI2014]重建——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3534 https://www.luogu.org/problemnew/show/P3317 T国 ...

  5. BZOJ2458:[BJOI2011]最小三角形——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2458 Description Xaviera现在遇到了一个有趣的问题. 平面上有N个点,Xavier ...

  6. cf 460 E. Congruence Equation 数学题

    cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...

  7. STL之四:list用法详解

    转载于:http://blog.csdn.net/longshengguoji/article/details/8520891 list容器介绍 相对于vector容器的连续线性空间,list是一个双 ...

  8. CCPC-Winter Camp div2 day1

    A:机器人 传送门:https://www.zhixincode.com/contest/1/problem/A 题意:地图是由A.B两根线组成的,机器人一开始是在A线上的S点,他初始时可以选择任意方 ...

  9. RotateAnimation 详解

    RotateAnimation 详解 看看新闻网>看引擎>开源产品 其他构造器的旋转也可参考这副图. RotateAnimation旋转坐标系为以旋转点为坐标系(0,0)点.x轴为0度,顺 ...

  10. Mybatis(4) 映射文件-参数处理

    参数处理: 单参数处理: mybatis 不会做任何特殊处理. #{key} : key 可以写任何字段取出参数值. 测试方法: mapper接口: mapper.xml: 控制台: 多参数处理: m ...