このファイル: /home/web6047/www/cgi-bin/prj/imgs of index/20200418-/simple.html
1
<html>
2
<head>
3
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
4
<title>CANVASベジェ曲線を3D回転する</title>
5
<script>console.clear();</script>
6
<script>
7
function onloadx() {
8
kaitenKakudo = 0;
9
setInterval( draw, 100 );
10
}
11
function draw() {
12
13
cc = document.getElementById( "test" ).getContext( "2d" );
14
cc.clearRect( 0, 0, cc.canvas.width, cc.canvas.height );
15
16
cc.save();
17
18
19
cc.translate( cc.canvas.width / 2, cc.canvas.height / 2 );
20
21
if( 1 ) {
22
23
cc.fillStyle = "red";
24
cc.fillRect( -2, -2, 4, 4 );
25
cc.fillText( " 原点", 0, 0 );
26
}
27
28
29
ax = 0; ay = 0;
30
bx = 0; by = -100;
31
cx = 0; cy = 50;
32
dx = 100; dy = -100;
33
34
35
cc.beginPath();
36
cc.moveTo( ax, ay );
37
cc.bezierCurveTo( bx, by, cx, cy, dx, dy );
38
39
cc.strokeStyle = "blue";
40
cc.stroke();
41
cc.strokeText( "2D", dx, dy );
42
43
44
45
46
AX = ax; AY = ay; AZ= 1000;
47
BX = bx; BY = by; BZ= 1000;
48
CX = cx; CY = cy; CZ= 1000;
49
DX = dx; DY = dy; DZ= 1000;
50
51
52
53
54
55
kaitenKakudo += 0.1;
56
57
58
CENTERX = 0;
59
CENTERY = 0;
60
CENTERZ = 1000;
61
62
63
64
65
66
67
if( 1 ) {
68
69
cc.save();
70
cc.fillStyle = "green";
71
cc.fillText( " 回転の中心軸", CENTERX, cc.canvas.height / 2 );
72
cc.beginPath();
73
cc.moveTo( CENTERX, -cc.canvas.height / 2 );
74
cc.lineTo( CENTERX, cc.canvas.height / 2 );
75
cc.strokeStyle = "green";
76
cc.setLineDash( [ 5, 5 ] );
77
cc.stroke();
78
cc.restore();
79
}
80
81
82
RES = rotate( CENTERX, CENTERZ, AX, AZ, kaitenKakudo );
83
AX = RES.X;
84
AZ = RES.Y;
85
86
87
RES = rotate( CENTERX, CENTERZ, BX, BZ, kaitenKakudo );
88
BX = RES.X;
89
BZ = RES.Y;
90
91
92
RES = rotate( CENTERX, CENTERZ, CX, CZ, kaitenKakudo );
93
CX = RES.X;
94
CZ = RES.Y;
95
96
97
RES = rotate( CENTERX, CENTERZ, DX, DZ, kaitenKakudo );
98
DX = RES.X;
99
DZ = RES.Y;
100
101
102
103
104
105
s = 50;
106
zoom = 20;
107
108
109
ah = AX * ( s / AZ ) * zoom;
110
av = AY * ( s / AZ ) * zoom;
111
112
113
114
115
116
117
118
119
bh = BX * ( s / BZ ) * zoom;
120
bv = BY * ( s / BZ ) * zoom;
121
122
123
ch = CX * ( s / CZ ) * zoom;
124
cv = CY * ( s / CZ ) * zoom;
125
126
127
dh = DX * ( s / DZ ) * zoom;
128
dv = DY * ( s / DZ ) * zoom;
129
130
131
132
cc.beginPath();
133
cc.moveTo( ah, av );
134
cc.bezierCurveTo( bh, bv, ch, cv, dh, dv );
135
136
cc.strokeStyle = "magenta";
137
cc.stroke();
138
cc.strokeText( "3D", dh, dv );
139
140
141
142
143
144
cc.restore();
145
}
146
function rotate( sx, sy, x, y, theta2 ) {
147
148
149
x -= sx;
150
y -= sy;
151
theta1 = Math.atan2( y, x );
152
hankei = Math.sqrt( x * x + y * y );
153
x = Math.cos( theta1 + theta2 ) * hankei;
154
y = Math.sin( theta1 + theta2 ) * hankei;
155
x += sx;
156
y += sy;
157
return { X: x, Y: y };
158
159
160
161
162
163
164
165
}
166
</script>
167
<style>
168
</style>
169
</head>
170
<body onload="onloadx()">
171
<canvas id="test" width="320" height="240" style="
172
width : 640px;
173
image-rendering: crisp-edges;
174
border : solid 1px black;
175
">
176
there is no canvas.
177
</canvas>
178
</body>
179
</html>