index-2.html
3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
	<title>无标题</title>
</head>
<body>
<h1>HTML Canvas Event Demo - By Gloomy Fish</h1>
<pre>Press W, A, S, D keys to move</pre>
<div id="my_painter">
    <canvas id="event_canvas" tabindex="0"></canvas>
</div>
<script type="text/javascript">
    var tempContext = null; // global variable 2d context
    var started = false;
    var mText_canvas = null;
    var x = 0, y =0;
    window.add
    window.onload = function() {
    var canvas = document.getElementById("event_canvas");
    console.log(canvas.parentNode.clientWidth);
    canvas.width = canvas.parentNode.clientWidth;
    canvas.height = canvas.parentNode.clientHeight;
    if (!canvas.getContext) {
    console.log("Canvas not supported. Please install a HTML5 compatible browser.");
    return;
    }
    // get 2D context of canvas and draw rectangel
    tempContext = canvas.getContext("2d");
    tempContext.fillStyle="blue";
    x = canvas.width/2;
    y = canvas.height/2;
    tempContext.fillRect(x, y, 80, 40);
    // key event - use DOM element as object
    canvas.addEventListener('keydown', doKeyDown, true);
    canvas.focus();
    // key event - use window as object
    window.addEventListener('keydown', doKeyDown, true);
    // mouse event
    canvas.addEventListener("mousedown", doMouseDown, false);
    canvas.addEventListener('mousemove', doMouseMove, false);
    canvas.addEventListener('mouseup', doMouseUp, false);
    }
    function getPointOnCanvas(canvas, x, y) {
    var bbox = canvas.getBoundingClientRect();
    return { x: x - bbox.left * (canvas.width / bbox.width),
    y: y - bbox.top * (canvas.height / bbox.height)
    };
    }
    function doKeyDown(e) {
    var keyID = e.keyCode ? e.keyCode :e.which;
    if(keyID === 38 || keyID === 87) { // up arrow and W
    clearCanvas();
    y = y - 10;
    tempContext.fillRect(x, y, 80, 40);
    e.preventDefault();
    }
    if(keyID === 39 || keyID === 68) { // right arrow and D
    clearCanvas();
    x = x + 10;
    tempContext.fillRect(x, y, 80, 40);
    e.preventDefault();
    }
    if(keyID === 40 || keyID === 83) { // down arrow and S
    clearCanvas();
    y = y + 10;
    tempContext.fillRect(x, y, 80, 40);
    e.preventDefault();
    }
    if(keyID === 37 || keyID === 65) { // left arrow and A
    clearCanvas();
    x = x - 10;
    tempContext.fillRect(x, y, 80, 40);
    e.preventDefault();
    }
    }
    function clearCanvas() {
    tempContext.clearRect(0, 0, 500, 500)
    }
    function doMouseDown(event) {
    var x = event.pageX;
    var y = event.pageY;
    var canvas = event.target;
    var loc = getPointOnCanvas(canvas, x, y);
    console.log("mouse down at point( x:" + loc.x + ", y:" + loc.y + ")");
    tempContext.beginPath();
    tempContext.moveTo(loc.x, loc.y);
    started = true;
    }
    function doMouseMove(event) {
        var x = event.pageX;
        var y = event.pageY;
        var canvas = event.target;
        var loc = getPointOnCanvas(canvas, x, y);
        if (started) {
            tempContext.lineTo(loc.x, loc.y);
            tempContext.stroke();
        }
    }
    function doMouseUp(event) {
    console.log("mouse up now");
    if (started) {
        doMouseMove(event);
        started = false;
        }
    }
</script>
</body>
</html>