[关闭]
@fantaghiro 2014-12-10T13:18:23.000000Z 字数 35424 阅读 2008

妙味课堂 - canvas

学习笔记 js 前端 妙味课堂 HTML5


标签

绘制环境

绘制方块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>canvas</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d'); //通过getContext('2d')才可以得到可以绘图的对象;webGL是支持3D绘图的
  15. // oGC.fillRect(50, 50, 100, 100);
  16. //oGC.strokeRect(50, 50, 100, 100); //边框默认为1像素,但仔细看,边框是2像素,因为边框绘制在50,50位置,向左延伸了0.5,向右延伸了0.5
  17. oGC.strokeRect(50.5, 50.5, 100, 100); //这时候边框就刚好是1像素了
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <canvas id="c1" width="400" height="400"> <!-- canvas的宽高最好在标签中设置,而不是在style中设置 -->
  23. <span>不支持canvas的浏览器可以看到这里的文字</span>
  24. </canvas> <!-- 默认:宽300 高150 -->
  25. </body>
  26. </html>

设置绘图

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>canvas</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.fillStyle = 'red';
  16. oGC.strokeStyle = 'blue';
  17. oGC.lineWidth = 10;
  18. //注意,下面两句话的顺序不同,出现的效果可能是不一样的,先写fill后写stroke,那么边框是压在填充上面的;如果先写stroke,后写fill,那么填充是盖在边框上面的
  19. oGC.fillRect(50, 50, 100, 100);
  20. oGC.strokeRect(50.5, 50.5, 100, 100);
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <canvas id="c1" width="400" height="400">
  26. <span>不支持canvas的浏览器可以看到这里的文字</span>
  27. </canvas>
  28. </body>
  29. </html>

边界绘制

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>canvas</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.fillStyle = 'red';
  16. oGC.strokeStyle = 'blue';
  17. oGC.lineWidth = 10;
  18. oGC.lineJoin = 'bevel';
  19. oGC.fillRect(50, 50, 100, 100);
  20. oGC.strokeRect(50.5, 50.5, 100, 100);
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <canvas id="c1" width="400" height="400">
  26. <span>不支持canvas的浏览器可以看到这里的文字</span>
  27. </canvas>
  28. </body>
  29. </html>

绘制路径

moveTo, lineTo, stroke, fill

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>canvas</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.beginPath();
  16. oGC.moveTo(100, 100);
  17. oGC.lineTo(200, 200);
  18. oGC.stroke();
  19. oGC.lineTo(300, 200);
  20. oGC.closePath(); //闭合路径
  21. oGC.stroke();
  22. //注意针对不同的绘制,进行开始和闭合的操作
  23. oGC.beginPath(); //如果不添加beginPath,后面的OGC.fill()也会作用于上面的那个三角形,添加了beginPath(),OGC.fill()就仅作用于当前的绘制
  24. oGC.moveTo(100, 200);
  25. oGC.lineTo(200, 300);
  26. oGC.stroke();
  27. oGC.lineTo(300, 300);
  28. oGC.closePath();
  29. oGC.fill();
  30. }
  31. </script>
  32. </head>
  33. <body>
  34. <canvas id="c1" width="400" height="400">
  35. <span>不支持canvas的浏览器可以看到这里的文字</span>
  36. </canvas>
  37. </body>
  38. </html>

rect

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>canvas</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.beginPath();
  16. oGC.rect(100, 100, 100, 100);
  17. oGC.closePath();
  18. oGC.stroke();
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <canvas id="c1" width="400" height="400">
  24. <span>不支持canvas的浏览器可以看到这里的文字</span>
  25. </canvas>
  26. </body>
  27. </html>

clearRect

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>canvas</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.beginPath();
  16. oGC.rect(100, 100, 100, 100);
  17. oGC.closePath();
  18. oGC.fill();
  19. oGC.clearRect(0, 0, oC.width, oC.height);
  20. }
  21. </script>
  22. </head>
  23. <body>
  24. <canvas id="c1" width="400" height="400">
  25. <span>不支持canvas的浏览器可以看到这里的文字</span>
  26. </canvas>
  27. </body>
  28. </html>

save()和restore()

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>canvas</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.save();
  16. oGC.fillStyle = 'red'; //这一句话封闭到了oGC.save()和oGC.restore()之中,不会影响到下面一个三角的fillStyle
  17. oGC.beginPath();
  18. oGC.moveTo(100, 100);
  19. oGC.lineTo(200, 200);
  20. oGC.lineTo(300, 200);
  21. oGC.closePath();
  22. oGC.fill();
  23. oGC.restore();
  24. oGC.beginPath();
  25. oGC.moveTo(100, 200);
  26. oGC.lineTo(200, 300);
  27. oGC.lineTo(300, 300);
  28. oGC.closePath();
  29. oGC.fill();
  30. }
  31. </script>
  32. </head>
  33. <body>
  34. <canvas id="c1" width="400" height="400">
  35. <span>不支持canvas的浏览器可以看到这里的文字</span>
  36. </canvas>
  37. </body>
  38. </html>

断点样式

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>canvas</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.lineWidth = 20;
  16. oGC.lineCap = 'square';
  17. oGC.moveTo(100, 100);
  18. oGC.lineTo(200, 200);
  19. oGC.stroke();
  20. }
  21. </script>
  22. </head>
  23. <body>
  24. <canvas id="c1" width="400" height="400">
  25. <span>不支持canvas的浏览器可以看到这里的文字</span>
  26. </canvas>
  27. </body>
  28. </html>

鼠标画线

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>鼠标画线</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oC.onmousedown = function(ev){
  16. var ev = ev || window.event;
  17. oGC.moveTo(ev.clientX-oC.offsetLeft, ev.clientY-oC.offsetTop);
  18. document.onmousemove = function(ev){
  19. var ev = ev || window.event;
  20. oGC.lineTo(ev.clientX-oC.offsetLeft, ev.clientY-oC.offsetTop);
  21. oGC.stroke();
  22. }
  23. document.onmouseup = function(){
  24. document.onmousemove = null;
  25. document.onmouseup = null;
  26. }
  27. }
  28. }
  29. </script>
  30. </head>
  31. <body>
  32. <canvas id="c1" width="400" height="400">
  33. <span>不支持canvas的浏览器可以看到这里的文字</span>
  34. </canvas>
  35. </body>
  36. </html>

方块移动

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>方块移动</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. var num = 0;
  16. oGC.fillRect(0, 0, 100, 100);
  17. setInterval(function(){
  18. num++;
  19. oGC.clearRect(0, 0, oC.width, oC.height);
  20. oGC.fillRect(num, num, 100, 100);
  21. }, 30)
  22. }
  23. </script>
  24. </head>
  25. <body>
  26. <canvas id="c1" width="400" height="400">
  27. <span>不支持canvas的浏览器可以看到这里的文字</span>
  28. </canvas>
  29. </body>
  30. </html>

在style中设置canvas宽高的问题

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>在style中设置canvas宽高的问题</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff; width: 400px; height: 400px;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.fillRect(0, 0, 100, 100); //把canvas的宽高写在style中,其实没有实际上更改canvas的宽高,而是将canvas进行等比例放大。因此这时候看起来这个方块就很大。
  16. //在canvas标签上进行的width和height的设置是真正宽高的设置;在style中给canvas设置的宽高,其实会让canvas从默认300*150的基础上等比例缩放。
  17. }
  18. </script>
  19. </head>
  20. <body>
  21. <canvas id="c1">
  22. <span>不支持canvas的浏览器可以看到这里的文字</span>
  23. </canvas>
  24. </body>
  25. </html>

绘制圆

绘制圆

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>画圆</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.moveTo(200, 200);
  16. //弧度 = 角度 * Math.PI/180
  17. oGC.arc(200, 200, 150, 0, 90*Math.PI/180, true);
  18. //oGC.closePath();
  19. oGC.stroke();
  20. }
  21. </script>
  22. </head>
  23. <body>
  24. <canvas id="c1" width="400" height="400"></canvas>
  25. </body>
  26. </html>

用arc绘制钟表

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>画钟表</title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. function toDraw(){
  16. var x = 200;
  17. var y = 200;
  18. var r = 150;
  19. oGC.clearRect(0, 0, oC.width, oC.height);
  20. var oDate = new Date();
  21. var oHours = oDate.getHours();
  22. var oMin = oDate.getMinutes();
  23. var oSec = oDate.getSeconds();
  24. var oHoursValue = (-90 + oHours*30 + oMin/2) * Math.PI/180;
  25. var oMinValue = (-90 + oMin*6) * Math.PI/180;
  26. var oSecValue = (-90 + oSec*6) * Math.PI/180;
  27. /*oGC.moveTo(x, y);
  28. oGC.arc(x, y, r, 0, 6*Math.PI/180, false);
  29. oGC.moveTo(x, y);
  30. oGC.arc(x, y, r, 6*Math.PI/180, 12*Math.PI/180, false);*/
  31. oGC.beginPath();
  32. for(var i=0; i<60; i++){
  33. oGC.moveTo(x, y);
  34. oGC.arc(x, y, r, 6*i*Math.PI/180, 6*(i+1)*Math.PI/180, false);
  35. }
  36. oGC.closePath();
  37. oGC.stroke();
  38. oGC.fillStyle = '#fff';
  39. oGC.beginPath();
  40. oGC.moveTo(x, y);
  41. oGC.arc(x, y, r*19/20, 0, 360*Math.PI/180);
  42. oGC.closePath();
  43. oGC.fill();
  44. oGC.lineWidth = 3;
  45. oGC.beginPath();
  46. for(var i=0; i<12; i++){
  47. oGC.moveTo(x, y);
  48. oGC.arc(x, y, r, 30*i*Math.PI/180, 30*(i+1)*Math.PI/180, false);
  49. }
  50. oGC.closePath();
  51. oGC.stroke();
  52. oGC.fillStyle = '#fff';
  53. oGC.beginPath();
  54. oGC.moveTo(x, y);
  55. oGC.arc(x, y, r*18/20, 0, 360*Math.PI/180);
  56. oGC.closePath();
  57. oGC.fill();
  58. //下面是画分针
  59. oGC.lineWidth = 5;
  60. oGC.beginPath();
  61. oGC.moveTo(x, y);
  62. oGC.arc(x, y, r*10/20, oHoursValue, oHoursValue, false); //起始点和终止点写同一个弧度,就是一条直线
  63. oGC.closePath();
  64. oGC.stroke();
  65. //下面是画时针
  66. oGC.lineWidth = 3;
  67. oGC.beginPath();
  68. oGC.moveTo(x, y);
  69. oGC.arc(x, y, r*14/20, oMinValue, oMinValue, false); //起始点和终止点写同一个弧度,就是一条直线
  70. oGC.closePath();
  71. oGC.stroke();
  72. //下面是画秒针
  73. oGC.lineWidth = 1;
  74. oGC.beginPath();
  75. oGC.moveTo(x, y);
  76. oGC.arc(x, y, r*18/20, oSecValue, oSecValue, false); //起始点和终止点写同一个弧度,就是一条直线
  77. oGC.closePath();
  78. oGC.stroke();
  79. }
  80. setInterval(toDraw, 1000);
  81. toDraw();
  82. }
  83. </script>
  84. </head>
  85. <body>
  86. <canvas id="c1" width="400" height="400"></canvas>
  87. </body>
  88. </html>

绘制其他曲线

arcTo()

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <title></title>
  5. <style>
  6. body {background: #000;}
  7. #c1 {background: #fff;}
  8. span {color: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. oGC.moveTo(100, 200);
  15. oGC.arcTo(100, 100, 200, 100, 50);
  16. oGC.stroke();
  17. }
  18. </script>
  19. </head>
  20. <body>
  21. <canvas id="c1" width="400" height="400"></canvas>
  22. </body>
  23. </html>

quadraticCurveTo()

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.moveTo(100, 200);
  16. oGC.quadraticCurveTo(100, 100, 200, 100);
  17. oGC.stroke();
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <canvas id="c1" width="400" height="400"></canvas>
  23. </body>
  24. </html>

bezierCurveTo()

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.moveTo(100, 200);
  16. oGC.bezierCurveTo(100, 100, 200, 200, 200, 100);
  17. oGC.stroke();
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <canvas id="c1" width="400" height="400"></canvas>
  23. </body>
  24. </html>

变换

变换的基本应用

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.translate(100, 100);
  16. oGC.rotate(45*Math.PI/180);
  17. oGC.scale(2, 2);
  18. oGC.fillRect(0, 0, 100, 100);
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <canvas id="c1" width="400" height="400"></canvas>
  24. </body>
  25. </html>

旋转的小方块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. var num = 0;
  16. setInterval(function(){
  17. num++;
  18. oGC.save(); //添加上save和restore,防止旋转角度的累加
  19. oGC.clearRect(0,0,oC.width,oC.height);
  20. oGC.translate(100, 100);
  21. oGC.rotate(num*Math.PI/180);
  22. oGC.translate(-50, -50); //让方块围绕中心旋转
  23. oGC.fillRect(0, 0, 100, 100);
  24. oGC.restore();
  25. }, 30)
  26. }
  27. </script>
  28. </head>
  29. <body>
  30. <canvas id="c1" width="400" height="400"></canvas>
  31. </body>
  32. </html>

缩放小方块

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. var num = 0;
  16. var num2 = 0;
  17. var value = 1;
  18. setInterval(function(){
  19. num++;
  20. oGC.save(); //添加上save和restore,防止旋转角度的累加
  21. oGC.clearRect(0,0,oC.width,oC.height);
  22. oGC.translate(100, 100);
  23. if(num2 == 75){
  24. value = -1;
  25. } else if(num2 == 25) {
  26. value = 1;
  27. }
  28. num2 += value;
  29. oGC.scale(num2*1/50, num2*1/50);
  30. oGC.rotate(num*Math.PI/180);
  31. oGC.translate(-50, -50); //让方块围绕中心旋转
  32. oGC.fillRect(0, 0, 100, 100);
  33. oGC.restore();
  34. }, 30)
  35. }
  36. </script>
  37. </head>
  38. <body>
  39. <canvas id="c1" width="400" height="400"></canvas>
  40. </body>
  41. </html>

插入图片

设置背景

drawImage()方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. //图片预加载的方法
  16. var yImg = new Image();
  17. yImg.onload = function(){
  18. draw(this);
  19. }
  20. yImg.src = 'images/2.png';
  21. function draw(obj){
  22. oGC.drawImage(obj, 0, 0, 400, 400);
  23. }
  24. }
  25. </script>
  26. </head>
  27. <body>
  28. <canvas id="c1" width="400" height="400"></canvas>
  29. </body>
  30. </html>

微博的图片旋转效果

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var aInput = document.getElementsByTagName('input');
  14. var oImg = document.getElementById('img1');
  15. var iNow = 0;
  16. var yImg = new Image();
  17. yImg.onload = function(){
  18. draw(oImg);
  19. }
  20. yImg.src = oImg.src;
  21. function draw(obj){
  22. var oC = document.createElement('canvas');
  23. var oGC = oC.getContext('2d');
  24. oC.width = obj.width;
  25. oC.height = obj.height;
  26. obj.parentNode.replaceChild(oC, obj)
  27. oGC.drawImage(obj, 0, 0);
  28. aInput[1].onclick = function(){
  29. //iNow记录点击了多少次
  30. if(iNow == 3){
  31. iNow = 0;
  32. } else {
  33. iNow++;
  34. }
  35. toChange(); //旋转图片的方法
  36. }
  37. aInput[0].onclick = function(){
  38. if(iNow == 0){
  39. iNow = 3;
  40. } else {
  41. iNow--;
  42. }
  43. toChange(); //旋转图片的方法
  44. }
  45. function toChange(){
  46. switch(iNow){
  47. case 1:
  48. oC.width = obj.height;
  49. oC.height = obj.width;
  50. oGC.rotate(90*Math.PI/180);
  51. oGC.drawImage(obj, 0, -obj.height);
  52. break;
  53. case 2:
  54. oC.width = obj.width;
  55. oC.height = obj.height;
  56. oGC.rotate(180*Math.PI/180);
  57. oGC.drawImage(obj, -obj.width, -obj.height);
  58. break;
  59. case 3:
  60. oC.width = obj.height;
  61. oC.height = obj.width;
  62. oGC.rotate(270*Math.PI/180);
  63. oGC.drawImage(obj, -obj.width, 0);
  64. break;
  65. case 0:
  66. oC.width = obj.width;
  67. oC.height = obj.height;
  68. oGC.rotate(0);
  69. oGC.drawImage(obj, 0, 0);
  70. break;
  71. }
  72. }
  73. }
  74. }
  75. </script>
  76. </head>
  77. <body>
  78. <input type="button" value="←">
  79. <input type="button" value="→">
  80. <div>
  81. <img src="images/2.png" id="img1">
  82. </div>
  83. </body>
  84. </html>

设置背景

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. var yImg = new Image();
  16. yImg.onload = function(){
  17. draw(this);
  18. }
  19. yImg.src = 'images/2.png';
  20. function draw(obj){
  21. var bg = oGC.createPattern(obj, 'repeat');
  22. oGC.fillStyle = bg;
  23. oGC.fillRect(0, 0, 300, 300);
  24. }
  25. }
  26. </script>
  27. </head>
  28. <body>
  29. <canvas id="c1" width="400" height="400"></canvas>
  30. </body>
  31. </html>

渐变

线性渐变

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. var obj = oGC.createLinearGradient(150, 100, 250, 200); //从起点坐标到终点坐标进行一个渐变
  16. obj.addColorStop(0, 'red'); //起点用0表示
  17. obj.addColorStop(0.5, 'yellow');
  18. obj.addColorStop(1, 'blue'); //终点用1表示
  19. oGC.fillStyle = obj;
  20. oGC.fillRect(150, 100, 100, 100);
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <canvas id="c1" width="400" height="400"></canvas>
  26. </body>
  27. </html>

径向渐变

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. var obj = oGC.createRadialGradient(200, 200, 100, 200, 200, 150); //从起点坐标到终点坐标进行一个渐变
  16. obj.addColorStop(0, 'red'); //起点用0表示
  17. obj.addColorStop(0.5, 'yellow');
  18. obj.addColorStop(1, 'blue'); //终点用1表示
  19. oGC.fillStyle = obj;
  20. oGC.fillRect(0, 0, oC.width, oC.height);
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <canvas id="c1" width="400" height="400"></canvas>
  26. </body>
  27. </html>

文本

文本的操作

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.font = '60px impact'; //两个参数必须要写
  16. oGC.textBaseline = 'top';
  17. oGC.fillText('妙味课堂', 0, 0);
  18. oGC.strokeText('妙味课堂', 0, 200);
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <canvas id="c1" width="400" height="400"></canvas>
  24. </body>
  25. </html>

文字居中

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.font = '60px impact';
  16. oGC.textBaseline = 'top'; //middle bottom
  17. var w = oGC.measureText('妙味课堂').width;
  18. oGC.fillText('妙味课堂', (oC.width - w) / 2, (oC.height - 60) / 2);
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <canvas id="c1" width="400" height="400"></canvas>
  24. </body>
  25. </html>

阴影

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. span {color: #fff;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.font = '60px impact';
  16. oGC.textBaseline = 'top';
  17. oGC.shadowOffsetX = 10;
  18. oGC.shadowOffsetY = 10;
  19. oGC.shadowBlur = 3;
  20. oGC.shadowColor = 'yellow'; //一定要设置阴影颜色,阴影才能显示出来。阴影的默认颜色是rgba(0,0,0,0)即黑色透明
  21. var w = oGC.measureText('妙味课堂').width;
  22. oGC.fillText('妙味课堂', (oC.width - w) / 2, (oC.height - 60) / 2);
  23. }
  24. </script>
  25. </head>
  26. <body>
  27. <canvas id="c1" width="400" height="400"></canvas>
  28. </body>
  29. </html>

像素

getImageData和putImageData

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. oGC.fillRect(0, 0, 100, 100);
  15. var oImg = oGC.getImageData(0, 0, 100, 100); //获取方块的所有像素的值
  16. //alert(oImg.width); //一行的像素个数
  17. //alert(oImg.height); //一列的像素个数
  18. //alert(oImg.data); //整体像素的数组集合
  19. //alert(oImg.data.length); //40000 这个方块应该是10000个像素。也就是数组中每四个值代表一个像素。一个像素中的四个值代表r/g/b/a。
  20. //alert(oImg.data[0]); //0 0-255 黑色到白色
  21. //alert(oImg.data[1]); //0 0-255 黑色到白色
  22. //alert(oImg.data[2]); //0 0-255 黑色到白色
  23. //alert(oImg.data[3]); //255 0-255 透明到不透明
  24. //也就是说第一个像素的rgba是0, 0, 0, 255,也就是不透明的黑色
  25. for(var i=0; i<oImg.width*oImg.height; i++){
  26. oImg.data[4*i] = 255;
  27. oImg.data[4*i+1] = 0;
  28. oImg.data[4*i+2] = 0;
  29. oImg.data[4*i+3] = 100;
  30. } //通过循环更改了像素颜色
  31. oGC.putImageData(oImg, 100, 100); //把oImg这样的像素信息添到了位于100,100的位置
  32. }
  33. </script>
  34. </head>
  35. <body>
  36. <canvas id="c1" width="400" height="400"></canvas>
  37. </body>
  38. </html>

createImageData

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. body {background: #000;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. var oImg = oGC.createImageData(100, 100);
  15. for(var i=0; i<oImg.width*oImg.height; i++){
  16. oImg.data[4*i] = 255;
  17. oImg.data[4*i+1] = 0;
  18. oImg.data[4*i+2] = 0;
  19. oImg.data[4*i+3] = 100;
  20. }
  21. oGC.putImageData(oImg, 100, 100);
  22. }
  23. </script>
  24. </head>
  25. <body>
  26. <canvas id="c1" width="400" height="400"></canvas>
  27. </body>
  28. </html>

像素显字;让文字中随机的10%的像素显示

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>像素显字</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. var aLi = document.getElementsByTagName('li');
  15. for(var i=0; i<aLi.length; i++){
  16. aLi[i].onclick = function(){
  17. var str = this.innerHTML;
  18. var h = 180;
  19. oGC.clearRect(0, 0, oC.width, oC.height);
  20. oGC.font = h + 'px impact';
  21. oGC.fillStyle = 'red';
  22. oGC.textBaseline = 'top';
  23. var w = oGC.measureText(str).width;
  24. oGC.fillText(str, (oC.width - w) / 2, (oC.height - h) / 2);
  25. var oImg = oGC.getImageData((oC.width - w) / 2, (oC.height - h) / 2, w, h);
  26. oGC.clearRect(0, 0, oC.width, oC.height);
  27. var arr = randomArr(w*h, w*h/10);
  28. var newImg = oGC.createImageData(w, h);
  29. for(var i = 0; i<arr.length; i++){
  30. newImg.data[arr[i]*4] = oImg.data[4*arr[i]];
  31. newImg.data[arr[i]*4+1] = oImg.data[4*arr[i]+1];
  32. newImg.data[arr[i]*4+2] = oImg.data[4*arr[i]+2];
  33. newImg.data[arr[i]*4+3] = oImg.data[4*arr[i]+3];
  34. }
  35. oGC.putImageData(newImg, (oC.width - w) / 2, (oC.height - h) / 2);
  36. }
  37. }
  38. function randomArr(iAll, iNow){ //从iAll个数里面随机取出iNow个数
  39. var arr = [];
  40. var newArr = [];
  41. for(var i=0; i<iAll; i++){
  42. arr.push(i);
  43. }
  44. for(var i=0; i<iNow; i++){
  45. newArr.push(arr.splice(Math.floor(Math.random()*arr.length), 1));
  46. }
  47. return newArr;
  48. }
  49. }
  50. </script>
  51. </head>
  52. <body>
  53. <canvas id="c1" width="400" height="400"></canvas>
  54. <ul style="float: left">
  55. <li></li>
  56. <li></li>
  57. <li></li>
  58. <li></li>
  59. </ul>
  60. </body>
  61. </html>

像素显字动画版

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>像素显字升级版</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. var aLi = document.getElementsByTagName('li');
  15. for(var i=0; i<aLi.length; i++){
  16. aLi[i].onclick = function(){
  17. var str = this.innerHTML;
  18. var h = 180;
  19. var timer = null;
  20. clearInterval(timer);
  21. var iNow = 0;
  22. oGC.clearRect(0, 0, oC.width, oC.height);
  23. oGC.font = h + 'px impact';
  24. oGC.fillStyle = 'red';
  25. oGC.textBaseline = 'top';
  26. var w = oGC.measureText(str).width;
  27. oGC.fillText(str, (oC.width - w) / 2, (oC.height - h) / 2);
  28. var oImg = oGC.getImageData((oC.width - w) / 2, (oC.height - h) / 2, w, h);
  29. oGC.clearRect(0, 0, oC.width, oC.height);
  30. var arr = randomArr(w*h, w*h/10);
  31. var newImg = oGC.createImageData(w, h);
  32. timer = setInterval(function(){
  33. for(var i = 0; i<arr[iNow].length; i++){
  34. newImg.data[arr[iNow][i]*4] = oImg.data[4*arr[iNow][i]];
  35. newImg.data[arr[iNow][i]*4+1] = oImg.data[4*arr[iNow][i]+1];
  36. newImg.data[arr[iNow][i]*4+2] = oImg.data[4*arr[iNow][i]+2];
  37. newImg.data[arr[iNow][i]*4+3] = oImg.data[4*arr[iNow][i]+3];
  38. }
  39. oGC.putImageData(newImg, (oC.width - w) / 2, (oC.height - h) / 2);
  40. if(iNow == 9){
  41. iNow = 0;
  42. clearInterval(timer);
  43. } else {
  44. iNow++;
  45. }
  46. }, 200)
  47. }
  48. }
  49. function randomArr(iAll, iNow){ //从iAll个数里面随机取出iNow个数
  50. var arr = [];
  51. var allArr = [];
  52. for(var i=0; i<iAll; i++){
  53. arr.push(i);
  54. }
  55. for(var j=0; j<iAll/iNow; j++){
  56. var newArr = []
  57. for(var i=0; i<iNow; i++){
  58. newArr.push(arr.splice(Math.floor(Math.random()*arr.length), 1));
  59. }
  60. allArr.push(newArr);
  61. }
  62. return allArr;
  63. }
  64. }
  65. </script>
  66. </head>
  67. <body>
  68. <canvas id="c1" width="400" height="400"></canvas>
  69. <ul style="float: left">
  70. <li></li>
  71. <li></li>
  72. <li></li>
  73. <li></li>
  74. </ul>
  75. </body>
  76. </html>

获取和设置指定坐标 封装函数getXY和setXY

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>getXY、setXY</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. oGC.fillRect(0, 0, 100, 100);
  15. var oImg = oGC.getImageData(0, 0, 100, 100);
  16. alert(getXY(oImg, 3, 5)); //得到坐标为3、5的rgba
  17. //setXY(oImg, 3, 5, [255, 0, 0, 255]); //设置坐标为3、5的rgba
  18. for(var i=0; i<oImg.width; i++){
  19. setXY(oImg, i, 5, [255, 0, 0, 255]);
  20. }
  21. oGC.putImageData(oImg, 100, 100);
  22. function getXY(obj, x, y){
  23. var w = obj.width;
  24. var h = obj.height;
  25. var d = obj.data;
  26. var color = [];
  27. color[0] = d[4*(y*w+x)];
  28. color[1] = d[4*(y*w+x)+1];
  29. color[2] = d[4*(y*w+x)+2];
  30. color[3] = d[4*(y*w+x)+3];
  31. return color;
  32. }
  33. function setXY(obj, x, y, color){
  34. var w = obj.width;
  35. var h = obj.height;
  36. var d = obj.data;
  37. d[4*(y*w+x)] = color[0];
  38. d[4*(y*w+x)+1] = color[1];
  39. d[4*(y*w+x)+2] = color[2];
  40. d[4*(y*w+x)+3] = color[3];
  41. }
  42. }
  43. </script>
  44. </head>
  45. <body>
  46. <canvas id="c1" width="400" height="400"></canvas>
  47. </body>
  48. </html>

图片的像素操作——反色、倒影、渐变

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>图片的像素操作</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. var yImg = new Image();
  15. yImg.onload = function(){
  16. draw(this);
  17. }
  18. yImg.src = 'img/2.png';
  19. function draw(obj){
  20. oC.width = obj.width;
  21. oGC.drawImage(obj, 0, 0);
  22. var oImg = oGC.getImageData(0, 0, obj.width, obj.height);
  23. var w = oImg.width;
  24. var h = oImg.height;
  25. var newImg = oGC.createImageData(obj.width, obj.height);
  26. //反色效果+倒影效果+渐变效果开始
  27. //反色,就是用255减去每个值
  28. for(var i=0; i<h; i++){ //循环每一列
  29. for(var j=0; j<w; j++){ //循环每一行
  30. var result = [];
  31. var color = getXY(oImg, j, i);
  32. result[0] = 255 - color[0];
  33. result[1] = 255 - color[1];
  34. result[2] = 255 - color[2];
  35. result[3] = 255*i/h;
  36. setXY(newImg, j, h-i, result);
  37. }
  38. }
  39. oGC.putImageData(newImg, 0, obj.height);
  40. //反色效果+倒影效果+渐变效果结束
  41. }
  42. function getXY(obj, x, y){
  43. var w = obj.width;
  44. var h = obj.height;
  45. var d = obj.data;
  46. var color = [];
  47. color[0] = d[4*(y*w+x)];
  48. color[1] = d[4*(y*w+x)+1];
  49. color[2] = d[4*(y*w+x)+2];
  50. color[3] = d[4*(y*w+x)+3];
  51. return color;
  52. }
  53. function setXY(obj, x, y, color){
  54. var w = obj.width;
  55. var h = obj.height;
  56. var d = obj.data;
  57. d[4*(y*w+x)] = color[0];
  58. d[4*(y*w+x)+1] = color[1];
  59. d[4*(y*w+x)+2] = color[2];
  60. d[4*(y*w+x)+3] = color[3];
  61. }
  62. }
  63. </script>
  64. </head>
  65. <body>
  66. <canvas id="c1" width="400" height="400"></canvas>
  67. </body>
  68. </html>

马赛克效果

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>图片的像素操作</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. var yImg = new Image();
  15. yImg.onload = function(){
  16. draw(this);
  17. }
  18. yImg.src = 'img/2.png';
  19. function draw(obj){
  20. oC.width = obj.width;
  21. oC.height = obj.height * 2;
  22. oGC.drawImage(obj, 0, 0);
  23. var oImg = oGC.getImageData(0, 0, obj.width, obj.height);
  24. var w = oImg.width;
  25. var h = oImg.height;
  26. var newImg = oGC.createImageData(obj.width, obj.height);
  27. var num = 10;
  28. var stepW = w/num;
  29. var stepH = h/num;
  30. //马赛克效果就是在num×num的像素里面随机取一个颜色,把这个num×num的区域都改成这个颜色
  31. for(var i=0; i<stepH; i++){
  32. for(var j=0; j<stepW; j++){
  33. var color = getXY(oImg, j*num+Math.floor(Math.random()*num), i*num+Math.floor(Math.random()*num));
  34. for(var k=0; k<num; k++){
  35. for(var l=0; l<num; l++){
  36. setXY(newImg, j*num+l, i*num+k, color);
  37. }
  38. }
  39. }
  40. }
  41. oGC.putImageData(newImg, 0, obj.height);
  42. }
  43. function getXY(obj, x, y){
  44. var w = obj.width;
  45. var h = obj.height;
  46. var d = obj.data;
  47. var color = [];
  48. color[0] = d[4*(y*w+x)];
  49. color[1] = d[4*(y*w+x)+1];
  50. color[2] = d[4*(y*w+x)+2];
  51. color[3] = d[4*(y*w+x)+3];
  52. return color;
  53. }
  54. function setXY(obj, x, y, color){
  55. var w = obj.width;
  56. var h = obj.height;
  57. var d = obj.data;
  58. d[4*(y*w+x)] = color[0];
  59. d[4*(y*w+x)+1] = color[1];
  60. d[4*(y*w+x)+2] = color[2];
  61. d[4*(y*w+x)+3] = color[3];
  62. }
  63. }
  64. </script>
  65. </head>
  66. <body>
  67. <canvas id="c1" width="400" height="400"></canvas>
  68. </body>
  69. </html>

合成

globalAlpha

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>合成</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. oGC.fillRect(0, 0, 100, 100);
  15. oGC.fillStyle = 'red';
  16. oGC.globalAlpha = 0.5; //在此之后操作的图形的透明度都会是半透明的
  17. oGC.fillRect(50, 50, 100, 100);
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <canvas id="c1" width="400" height="400"></canvas>
  23. </body>
  24. </html>

元素与元素叠加

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>合成</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. oGC.fillRect(0, 0, 100, 100);
  15. oGC.fillStyle = 'red';
  16. //source就是新画的,destination就是先画的
  17. //oGC.globalCompositeOperation = 'destination-over';
  18. //oGC.globalCompositeOperation = 'source-atop';
  19. oGC.globalCompositeOperation = 'xor';
  20. oGC.fillRect(50, 50, 100, 100); //默认后画的会盖在先画的前面。改变覆盖顺序用globalCompositeOperation属性
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <canvas id="c1" width="400" height="400"></canvas>
  26. </body>
  27. </html>

将画布导出为图像

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>画布导出为图像</title>
  6. <style>
  7. body {background: blue; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oImg = document.getElementById('img1');
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. oGC.fillRect(0, 0, 100, 100);
  16. oGC.fillStyle = 'red';
  17. oGC.globalCompositeOperation = 'xor';
  18. oGC.fillRect(50, 50, 100, 100);
  19. //alert(oC.toDataURL());
  20. oImg.src = oC.toDataURL();
  21. }
  22. </script>
  23. </head>
  24. <body>
  25. <canvas id="c1" width="400" height="400"></canvas>
  26. <img id="img1" src="">
  27. </body>
  28. </html>

事件操作

isPointInPath方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>事件操作</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. oGC.beginPath();
  15. oGC.arc(100, 100, 50, 0, 360*Math.PI/180, false);
  16. oGC.closePath();
  17. oGC.fill();
  18. oGC.beginPath();
  19. oGC.arc(200, 200, 50, 0, 360*Math.PI/180, false);
  20. oGC.closePath();
  21. oGC.fill();
  22. //点击圆以内的部分,弹出123
  23. oC.onmousedown = function(ev){
  24. var ev = ev || window.event;
  25. var x = ev.clientX - oC.offsetLeft;
  26. var y = ev.clientY - oC.offsetTop;
  27. if(oGC.isPointInPath(x, y)){
  28. //isPointInPath方法只针对最后一次画出来的那个图形
  29. alert(123);
  30. }
  31. }
  32. }
  33. </script>
  34. </head>
  35. <body>
  36. <canvas id="c1" width="400" height="400"></canvas>
  37. </body>
  38. </html>

点击不同的图形,执行不同的操作

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>事件操作</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script>
  11. window.onload = function(){
  12. var oC = document.getElementById('c1');
  13. var oGC = oC.getContext('2d');
  14. var c1 = new Shape(100, 100, 50);
  15. var c2 = new Shape(200, 200, 50);
  16. oC.onmousedown = function(ev){
  17. var ev = ev || window.event;
  18. var point = {
  19. x : ev.clientX - oC.offsetLeft,
  20. y: ev.clientY - oC.offsetTop
  21. }
  22. c1.reDraw(point);
  23. c2.reDraw(point);
  24. }
  25. c1.click = function(){
  26. alert(123);
  27. }
  28. c2.click = function(){
  29. alert(456);
  30. }
  31. //要通过对象来实现。点击某个图形的时候,先找到这个图形对应的对象,然后再让这个图形重绘一下,然后就可以使用isPointInPath方法了,因为这个方法只能针对最后一个绘制的图形起作用。
  32. function Shape(x, y, r){
  33. this.x = x;
  34. this.y = y;
  35. this.r = r;
  36. oGC.beginPath();
  37. oGC.arc(this.x, this.y, this.r, 0, 360*Math.PI/180, false);
  38. oGC.closePath();
  39. oGC.fill();
  40. }
  41. Shape.prototype.reDraw = function(point){
  42. oGC.beginPath();
  43. oGC.arc(this.x, this.y, this.r, 0, 360*Math.PI/180, false);
  44. oGC.closePath();
  45. oGC.fill();
  46. if(oGC.isPointInPath(point.x, point.y)){
  47. this.click();
  48. }
  49. }
  50. }
  51. </script>
  52. </head>
  53. <body>
  54. <canvas id="c1" width="400" height="400"></canvas>
  55. </body>
  56. </html>

jCanvaScript的绘制和点击操作

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>jCanvaScript的使用</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script src="jCanvaScript.1.5.18.min.js"></script>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. jc.start('c1', true); //开始 把canvas的id传进去 第二个参数代表“重绘”,也就是当有事件的时候,它会重新绘制
  16. //jc.rect(100, 100, 50, 50, '#ff0000' , true); //画一个方形
  17. //jc.circle(100, 100, 50, '#ff0000', 1); //画一个圆形
  18. jc.circle(100, 100, 50, '#ff0000', 1).click(function(){
  19. alert(123);
  20. });
  21. jc.start('c1'); //闭合
  22. }
  23. </script>
  24. </head>
  25. <body>
  26. <canvas id="c1" width="400" height="400"></canvas>
  27. </body>
  28. </html>

用jCanvaScript实现拖拽

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>jCanvaScript的使用</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script src="jCanvaScript.1.5.18.min.js"></script>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. jc.start('c1', true);
  16. jc.circle(100, 100, 50, '#ff0000', 1).draggable();
  17. jc.start('c1');
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <canvas id="c1" width="400" height="400"></canvas>
  23. </body>
  24. </html>

通过jCanvaScript让外部按钮控制画布

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>jCanvaScript的使用</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script src="jCanvaScript.1.5.18.min.js"></script>
  11. <script>
  12. window.onload = function(){
  13. var oInput = document.getElementById('input1');
  14. var oC = document.getElementById('c1');
  15. var oGC = oC.getContext('2d');
  16. jc.start('c1', true);
  17. jc.circle(100, 100, 50, '#ff0000', 1).id('circle1');
  18. jc.start('c1');
  19. oInput.onclick = function(){
  20. jc('#circle1').color('#ffff00');
  21. }
  22. }
  23. </script>
  24. </head>
  25. <body>
  26. <canvas id="c1" width="400" height="400"></canvas>
  27. <input type="button" value="点击" id="input1">
  28. </body>
  29. </html>

通过jCanvaScript实现运动效果

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>jCanvaScript的使用</title>
  6. <style>
  7. body {background: #000; color: white; font-size: 30px;}
  8. #c1 {background: #fff;}
  9. </style>
  10. <script src="jCanvaScript.1.5.18.min.js"></script>
  11. <script>
  12. window.onload = function(){
  13. var oInput = document.getElementById('input1');
  14. var oC = document.getElementById('c1');
  15. var oGC = oC.getContext('2d');
  16. jc.start('c1', true);
  17. jc.circle(100, 100, 50, '#ff0000', 1).id('circle1');
  18. jc.start('c1');
  19. oInput.onclick = function(){
  20. jc('#circle1').color('#ffff00').animate({x: 200, y: 200, radius: 5}, 2000);
  21. }
  22. }
  23. </script>
  24. </head>
  25. <body>
  26. <canvas id="c1" width="400" height="400"></canvas>
  27. <input type="button" value="点击" id="input1">
  28. </body>
  29. </html>

简易祖玛小游戏

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>简易祖玛小游戏</title>
  6. <style>
  7. * {margin: 0; padding: 0;}
  8. body {background: black;}
  9. #div1 {background: white; width: 600px; margin: 20px auto;}
  10. </style>
  11. <script>
  12. window.onload = function(){
  13. var oC = document.getElementById('c1');
  14. var oGC = oC.getContext('2d');
  15. var i = 0;
  16. var yImg = new Image();
  17. yImg.src = 'person.png';
  18. yImg.onload = function(){
  19. setInterval(function(){
  20. oGC.clearRect(0, 0, oC.width, oC.height);
  21. oGC.beginPath();
  22. oGC.arc(300, 200, 200, -90*Math.PI/180, 180*Math.PI/180, false);
  23. oGC.stroke();
  24. oGC.beginPath();
  25. oGC.arc(250, 200, 150, 180*Math.PI/180, 360*Math.PI/180);
  26. oGC.stroke();
  27. oGC.beginPath();
  28. oGC.arc(400, 200, 20, 0, 360*Math.PI/180);
  29. oGC.stroke();
  30. for(var i=0; i<ball.length; i++){
  31. oGC.beginPath();
  32. oGC.moveTo(ball[i].x, ball[i].y);
  33. oGC.arc(ball[i].x, ball[i].y, 20, 0, 360*Math.PI/180, false);
  34. oGC.fill();
  35. }
  36. oGC.save()
  37. oGC.translate(300, 200);
  38. oGC.rotate(iRotate);
  39. oGC.translate(-40, -40);
  40. oGC.drawImage(yImg, 0, 0);
  41. oGC.restore();
  42. for(var i=0; i<bullet.length; i++){
  43. oGC.save();
  44. oGC.fillStyle = 'red';
  45. oGC.beginPath();
  46. oGC.moveTo(bullet[i].x, bullet[i].y);
  47. oGC.arc(bullet[i].x, bullet[i].y, 20, 0, 360*Math.PI/180, false);
  48. oGC.fill();
  49. oGC.restore();
  50. }
  51. oGC.save();
  52. oGC.font = '60px impact';
  53. oGC.textBaseline = 'top';
  54. oGC.fillStyle = 'red';
  55. oGC.shadowOffsetX = 10;
  56. oGC.shadowOffsetY = 10;
  57. oGC.shadowColor = 'green';
  58. oGC.shadowBlur = 5;
  59. var w = oGC.measureText('简易祖玛').width;
  60. var h = 60;
  61. oGC.fillText('简易祖玛', (oC.width-w)/2, 450);
  62. oGC.restore();
  63. }, 1000/60);
  64. setInterval(function(){
  65. for(var i=0; i<ball.length; i++){
  66. ball[i].num++;
  67. if(ball[i].num == 270){
  68. ball[i].r = 150;
  69. ball[i].startX = 250;
  70. ball[i].startY = 50;
  71. }
  72. if(ball[i].num == 270 + 180){
  73. alert('游戏结束');
  74. window.location.reload(); //刷新一下页面
  75. }
  76. ball[i].x = Math.sin(ball[i].num*Math.PI/180) * ball[i].r + ball[i].startX;
  77. ball[i].y = ball[i].r - Math.cos(ball[i].num*Math.PI/180) * ball[i].r + ball[i].startY;
  78. }
  79. for(var i=0; i<bullet.length; i++){
  80. bullet[i].x = bullet[i].x + bullet[i].sX;
  81. bullet[i].y = bullet[i].y + bullet[i].sY;
  82. }
  83. for(var i=0; i<bullet.length; i++){
  84. for(var j=0; j<ball.length; j++){
  85. if(pz(bullet[i].x, bullet[i].y, ball[j].x, ball[j].y)){
  86. bullet.splice(i, 1);
  87. ball.splice(j, 1);
  88. break;
  89. }
  90. }
  91. }
  92. }, 30)
  93. var ball = [];
  94. ball[0] = {
  95. x : 300,
  96. y : 0,
  97. r : 200,
  98. num : 0, //角度
  99. startX : 300,
  100. startY : 0
  101. };
  102. setInterval(function(){
  103. ball.push({
  104. x : 300,
  105. y : 0,
  106. r : 200,
  107. num : 0, //角度
  108. startX : 300,
  109. startY : 0
  110. })
  111. }, 350);
  112. var iRotate = 0;
  113. oC.onmousemove = function(ev){
  114. var ev = ev || window.event;
  115. var x = ev.clientX - oC.offsetLeft;
  116. var y = ev.clientY - oC.offsetTop;
  117. var a = x - 300;
  118. var b = y - 200;
  119. var c = Math.sqrt(a*a + b*b);
  120. if(a>0 && b>0){
  121. iRotate = Math.asin(b/c) + 90*Math.PI/180;
  122. } else if(a>0){
  123. iRotate = Math.asin(a/c);
  124. }
  125. if(a<0 && b>0){
  126. iRotate = -(Math.asin(b/c) + 90*Math.PI/180);
  127. } else if(a<0){
  128. iRotate = Math.asin(a/c);
  129. }
  130. }
  131. var bullet = [];
  132. oC.onmousedown = function(ev){
  133. var ev = ev || window.event;
  134. var x = ev.clientX - oC.offsetLeft;
  135. var y = ev.clientY - oC.offsetTop;
  136. var a = x - 300;
  137. var b = y - 200;
  138. var c = Math.sqrt(a*a + b*b);
  139. var speed = 5;
  140. var sX = speed * a/c;
  141. var sY = speed * b/c;
  142. bullet.push({
  143. x : 300,
  144. y : 200,
  145. sX : sX,
  146. sY : sY
  147. });
  148. }
  149. }
  150. function pz(x1, y1, x2, y2){ //球的碰撞检测 检测两个圆心之间的距离是否小于两个圆半径之和
  151. var a = x1 - x2;
  152. var b = y1 - y2;
  153. var c = Math.sqrt(a*a + b*b);
  154. if(c<40){
  155. return true;
  156. } else {
  157. return false;
  158. }
  159. }
  160. }
  161. </script>
  162. </head>
  163. <body>
  164. <div id="div1">
  165. <canvas id="c1" width="600" height="600"></canvas>
  166. </div>
  167. </body>
  168. </html>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注