http://www.lampbrother.net
54. tempContext.fillRect(x, y, 80, 40); 55. e.preventDefault(); 56. }
57. if(keyID === 40 || keyID === 83) { // down arrow and S 58. clearCanvas(); 59. y = y + 10;
60. tempContext.fillRect(x, y, 80, 40); 61. e.preventDefault(); 62. }
63. if(keyID === 37 || keyID === 65) { // left arrow and A 64. clearCanvas(); 65. x = x - 10;
66. tempContext.fillRect(x, y, 80, 40); 67. e.preventDefault(); 68. } 69. } 70.
71. function clearCanvas() {
72. tempContext.clearRect(0, 0, 500, 500) 73. } 74.
75. function doMouseDown(event) { 76. var x = event.pageX; 77. var y = event.pageY;
78. var canvas = event.target;
79. var loc = getPointOnCanvas(canvas, x, y);
80. console.log(\ + loc.x + \ + loc.y + \); 81. tempContext.beginPath();
82. tempContext.moveTo(loc.x, loc.y); 83. started = true; 84. } 85.
86. function doMouseMove(event) { 87. var x = event.pageX; 88. var y = event.pageY;
89. var canvas = event.target;
90. var loc = getPointOnCanvas(canvas, x, y); 91. if (started) {
92. tempContext.lineTo(loc.x, loc.y); 93. tempContext.stroke(); 94. } 95. } 96.
97. function doMouseUp(event) {
http://www.lampbrother.net
98. console.log(\); 99. if (started) {
100. doMouseMove(event); 101. started = false; 102. } 103. }
HTML部分:
[html] view plain copy
1. 2. 3. 4. 5. 6. 7.
HTML Canvas Event Demo - By Gloomy Fish
Press W, A, S, D keys to move
觉得不错请支持一些,谢谢!!