-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprite.html
123 lines (97 loc) · 2.78 KB
/
sprite.html
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
120
121
122
123
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>精灵</title>
<style>
#canvas {
background: #fefefe;
margin-top: 20px;
margin-left: 20px;
border: thin solid lightgray;
-webkit-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
-moz-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
}
</style>
</head>
<body>
<canvas id="canvas" width="650" height="375"></canvas>
<script>
var Sprite = function(name,painter,behaviors){
this.name = name || '';
this.painter = painter || '';
this.top = 0;
this.left = 0;
this.width = 10;
this.height = 10;
this.velocityX = 0;
this.velocityY = 0;
this.visible = true;
this.animating = false;
this.behaviors = behaviors || [];
return this;
}
Sprite.prototype = {
paint: function(context){
if(this.painter !== undefined && this.visible){
this.painter.paint(this, context)
}
},
update: function(context,time){
for(var i = 0; i < this.behaviors.length; ++i){
this.behaviors[i].execute(this, context, time);
}
}
}
var context = document.getElementById('canvas').getContext('2d'),
RADIUS = 75,
ball = new Sprite('ball',{
paint: function(sprite,context){
context.beginPath();
context.arc(sprite.left + sprite.width / 2,
sprite.top + sprite.height / 2,
RADIUS, 0 ,Math.PI * 2, false
);
context.clip(); // 阴影只显示在裁剪区域内
context.shadowColor = 'rgb(0,0,0)';
context.shadowOffsetX = -4;
context.shadowOffsetY = -4;
context.shadowBlur = 8;
context.lineWidth = 2;
context.strokeStyle = 'rgb(100,100,195)';
context.fillStyle = 'rgba(30,144,255,0.15)';
context.fill();
context.stroke();
}
});
function drawGrid(color,stepx,stepy){
context.save()
context.shadowColor = undefined;
context.shadowOffsetX = 0;
context.shadowOffsetY = 0;
context.strokeStyle = color;
context.fillStyle = '#ffffff';
context.lineWidth = 0.5;
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
for (var i = stepx + 0.5; i < context.canvas.width; i += stepx) {
context.beginPath();
context.moveTo(i, 0);
context.lineTo(i, context.canvas.height);
context.stroke();
}
for (var i = stepy + 0.5; i < context.canvas.height; i += stepy) {
context.beginPath();
context.moveTo(0, i);
context.lineTo(context.canvas.width, i);
context.stroke();
}
context.restore();
}
drawGrid('lightgray',10,10);
ball.left = 320;
ball.top = 160;
ball.paint(context);
</script>
</body>
</html>