练习1_画板.html 891 B

12345678910111213141516171819202122232425262728293031323334
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. #can{
  9. background-color: #000;
  10. /* margin-left: 300px; */
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <canvas id="can" width="400" height="400"></canvas>
  16. <script>
  17. var can = document.querySelector('#can');
  18. var ctx = can.getContext('2d');
  19. ctx.strokeStyle = '#fff';
  20. ctx.lineWidth = 3;
  21. can.onmousedown = function(e){
  22. ctx.moveTo(e.offsetX,e.offsetY);
  23. can.onmousemove = function(e){
  24. ctx.lineTo(e.offsetX,e.offsetY);
  25. ctx.stroke();
  26. }
  27. }
  28. document.documentElement.onmouseup = function(){
  29. can.onmousemove = null;
  30. }
  31. </script>
  32. </body>
  33. </html>