質問編集履歴

1

現在のコードを追記します。jquery使っています。Chromeで動作検証しています。

2019/09/04 05:25

投稿

kittycut
kittycut

スコア9

test CHANGED
File without changes
test CHANGED
@@ -47,3 +47,521 @@
47
47
  代替案など、どんな情報でもいただけるとありがたいです。
48
48
 
49
49
  よろしくお願いします。
50
+
51
+
52
+
53
+ ## 追記・現在のコード
54
+
55
+ 参考のリンクそのままです。文字数ではじかれてしまったので、jsは②だけ載せておきます。他に①のjsがあり、2つのjsを読み込んでいます。
56
+
57
+ ```html
58
+
59
+ <?xml version="1.0" encoding="utf-8"?>
60
+
61
+ <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:ibooks="http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0" epub:prefix="ibooks: http://vocabulary.itunes.apple.com/rdf/ibooks/vocabulary-extensions-1.0">
62
+
63
+ <head>
64
+
65
+ <title>test</title>
66
+
67
+ <meta name="viewport" content="width=1024, height=768"/>
68
+
69
+ <meta charset="UTF-8"/>
70
+
71
+ <script src="js/media-overlay.js" type="text/javascript"></script>
72
+
73
+ <script src="js/jquery-3.3.1.min.js" type="text/javascript"></script>
74
+
75
+ <script src="js/ibooks.js" type="text/javascript"></script>
76
+
77
+
78
+
79
+ <link href="css/stylesheet.css" type="text/css" rel="stylesheet"/>
80
+
81
+ <link href="css/page.css" type="text/css" rel="stylesheet"/>
82
+
83
+ </head>
84
+
85
+
86
+
87
+ <body>
88
+
89
+ <div class="page">
90
+
91
+ <div id="rice_field">
92
+
93
+ <canvas id="canvas1" width="1020" height="300"></canvas>
94
+
95
+ <canvas id="canvas2" width="1020" height="300"></canvas>
96
+
97
+ </div>
98
+
99
+ </div>
100
+
101
+ <script src="js/page.js" type="text/javascript"></script>
102
+
103
+ <script src="js/page_2.js" type="text/javascript"></script>
104
+
105
+ </body>
106
+
107
+ </html>
108
+
109
+ ```
110
+
111
+ ```js
112
+
113
+ var canvas = document.getElementById("canvas2");
114
+
115
+ var ctx = canvas.getContext("2d");
116
+
117
+ var canvasOffset = $("#canvas2").offset();
118
+
119
+ var offsetX = canvasOffset.left;
120
+
121
+ var offsetY = canvasOffset.top;
122
+
123
+ var cHeight = canvas.height;
124
+
125
+ var showControls = true;
126
+
127
+ var lastMouseX = 0;
128
+
129
+ var lastTouchX = 0;
130
+
131
+ // preset styling CONSTANTS
132
+
133
+ var SWAY = .55; // max endpoint sway from center
134
+
135
+ var C1Y = .10; // fixed Y of cp#1
136
+
137
+ var C2SWAY = .20 // max cp#2 sway from center
138
+
139
+ var C2Y = .75; // fixed Y of cp#2
140
+
141
+ var YY = 60; // max height of ellipse at top of hair
142
+
143
+ var PIPERCENT = Math.PI / 100;
144
+
145
+ var hairs = [];
146
+
147
+ // create hairs
148
+
149
+ var newHairX = 100;
150
+
151
+ var hairCount = 20;
152
+
153
+ for (var i = 0; i < hairCount; i++) {
154
+
155
+ var randomLength = 100 + parseInt(Math.random() * 20);
156
+
157
+ addHair(newHairX + (i * 30), randomLength);
158
+
159
+ }
160
+
161
+ function addHair(x, length) {
162
+
163
+ hairs.push({
164
+
165
+ x: x,
166
+
167
+ length: length,
168
+
169
+ left: 0,
170
+
171
+ right: 0,
172
+
173
+ top: 0,
174
+
175
+ s: {
176
+
177
+ x: 0,
178
+
179
+ y: 0
180
+
181
+ },
182
+
183
+ c1: {
184
+
185
+ x: 0,
186
+
187
+ y: 0
188
+
189
+ },
190
+
191
+ c2: {
192
+
193
+ x: 0,
194
+
195
+ y: 0
196
+
197
+ },
198
+
199
+ e: {
200
+
201
+ x: 0,
202
+
203
+ y: 0
204
+
205
+ },
206
+
207
+ isInMotion: false,
208
+
209
+ currentX: 0
210
+
211
+ });
212
+
213
+ }
214
+
215
+ for (var i = 0; i < hairs.length; i++) {
216
+
217
+ var h = hairs[i];
218
+
219
+ setHairPointsFixed(h);
220
+
221
+ setHairPointsPct(h, 50);
222
+
223
+ draw(h);
224
+
225
+ }
226
+
227
+ function setHairPointsFixed(h) {
228
+
229
+ h.s.x = h.x;
230
+
231
+ h.s.y = cHeight;
232
+
233
+ h.c1.x = h.x;
234
+
235
+ h.c1.y = cHeight - h.length * C1Y;
236
+
237
+ h.c2.y = cHeight - h.length * C2Y;
238
+
239
+ h.top = cHeight - h.length;
240
+
241
+ h.left = h.x - h.length * SWAY;
242
+
243
+ h.right = h.x + h.length * SWAY;
244
+
245
+ }
246
+
247
+ function setHairPointsPct(h, pct) {
248
+
249
+ // endpoint
250
+
251
+ var a = Math.PI + PIPERCENT * pct;
252
+
253
+ h.e.x = h.x - ((h.length * SWAY) * Math.cos(a));
254
+
255
+ h.e.y = h.top + (YY * Math.sin(a));
256
+
257
+ // controlpoint#2
258
+
259
+ h.c2.x = h.x + h.length * (C2SWAY * 2 * pct / 100 - C2SWAY);
260
+
261
+ }
262
+
263
+
264
+
265
+ //////////////////////////////
266
+
267
+ $("#canvas2").on('mousemove', function(e) {
268
+
269
+ mouseX = parseInt(e.clientX - offsetX);
270
+
271
+ mouseY = parseInt(e.clientY - offsetY);
272
+
273
+
274
+
275
+ // draw this frame based on mouse moves
276
+
277
+ ctx.clearRect(0, 0, cWidth, cHeight);
278
+
279
+ for (var i = 0; i < hairs.length; i++)
280
+
281
+ {hairMoves(hairs[i], mouseX, mouseY);
282
+
283
+ }
284
+
285
+
286
+
287
+ lastMouseX = mouseX;
288
+
289
+ })
290
+
291
+ $("#canvas2").on('touchstart touchmove', function(e) {
292
+
293
+ e.preventDefault();
294
+
295
+ touchX = parseInt(e.changedTouches[0].clientX - offsetX);
296
+
297
+ touchY = parseInt(e.changedTouches[0].clientY - offsetY);
298
+
299
+ // draw this frame based on touchmoves
300
+
301
+ ctx.clearRect(0, 0, cWidth, cHeight);
302
+
303
+ for (var i = 0; i < hairs.length; i++) {
304
+
305
+ hairMoves2(hairs[i], touchX, touchY);
306
+
307
+
308
+
309
+ };
310
+
311
+ lastTouchX = touchX;
312
+
313
+ });
314
+
315
+
316
+
317
+ function hairMoves(h, mouseX, mouseY) {
318
+
319
+
320
+
321
+ // No hair movement if not touching hair
322
+
323
+ if (mouseY < cHeight - h.length - YY) {
324
+
325
+ if (h.isInMotion) {
326
+
327
+ h.isInMotion = false;
328
+
329
+ setHairPointsPct(h, 50);
330
+
331
+ }
332
+
333
+ draw(h);
334
+
335
+ return;
336
+
337
+ }
338
+
339
+ // No hair movement if too deep in hair
340
+
341
+ if (mouseY > h.c1.y) {
342
+
343
+ draw(h);
344
+
345
+ return;
346
+
347
+ }
348
+
349
+ //
350
+
351
+ var pct = 50;
352
+
353
+ if (mouseX >= h.left && mouseX <= h.right) {
354
+
355
+
356
+
357
+ if (h.isInMotion) {
358
+
359
+ var pct = -(mouseX - h.right) / (h.right - h.left) * 100;
360
+
361
+ setHairPointsPct(h, pct);
362
+
363
+ draw(h);
364
+
365
+ } else {
366
+
367
+ // if hair is at rest
368
+
369
+ // but mouse has just contacted hair
370
+
371
+ // set hair in motion
372
+
373
+ if ((lastMouseX <= h.x && mouseX >= h.x) || (lastMouseX >= h.x && mouseX <= h.x)) {
374
+
375
+ h.isInMotion = true;
376
+
377
+ var pct = -(mouseX - h.right) / (h.right - h.left) * 100;
378
+
379
+ }
380
+
381
+ setHairPointsPct(h, pct);
382
+
383
+ draw(h);
384
+
385
+
386
+
387
+ }
388
+
389
+
390
+
391
+ } else {
392
+
393
+ if (h.isInMotion) {
394
+
395
+ h.isInMotion = false;
396
+
397
+ setHairPointsPct(h, 50);
398
+
399
+ };
400
+
401
+ draw(h);
402
+
403
+
404
+
405
+ }
406
+
407
+ }
408
+
409
+
410
+
411
+ function hairMoves2(h, touchX, touchY) {
412
+
413
+
414
+
415
+ // No hair movement if not touching hair
416
+
417
+ if (touchY < cHeight - h.length - YY) {
418
+
419
+ if (h.isInMotion) {
420
+
421
+ h.isInMotion = false;
422
+
423
+ setHairPointsPct(h, 50);
424
+
425
+ }
426
+
427
+ draw(h);
428
+
429
+ return;
430
+
431
+ }
432
+
433
+
434
+
435
+ // No hair movement if too deep in hair
436
+
437
+ if (touchY > h.c2.y) {
438
+
439
+ draw(h);
440
+
441
+ return;
442
+
443
+ }
444
+
445
+ //
446
+
447
+ var pct = 50;
448
+
449
+ if (touchX >= h.left && touchX <= h.right) {
450
+
451
+ if (h.isInMotion) {
452
+
453
+ var pct = -(touchX - h.right) / (h.right - h.left) * 100;
454
+
455
+ setHairPointsPct(h, pct);
456
+
457
+ draw(h);
458
+
459
+ } else {
460
+
461
+ // if hair is at rest
462
+
463
+ // but mouse has just contacted hair
464
+
465
+ // set hair in motion
466
+
467
+ if ((lastTouchX <= h.x && touchX >= h.x) || (lastTouchX >= h.x && touchX <= h.x)) {
468
+
469
+ h.isInMotion = true;
470
+
471
+ var pct = -(touchX - h.right) / (h.right - h.left) * 100;
472
+
473
+ }
474
+
475
+ setHairPointsPct(h, pct);
476
+
477
+ draw(h);
478
+
479
+
480
+
481
+ }
482
+
483
+
484
+
485
+ } else {
486
+
487
+ if (h.isInMotion) {
488
+
489
+ h.isInMotion = false;
490
+
491
+ setHairPointsPct(h, 50);
492
+
493
+ };
494
+
495
+ draw(h);
496
+
497
+
498
+
499
+ }
500
+
501
+ }
502
+
503
+
504
+
505
+ function dot(pt, color) {
506
+
507
+ ctx.beginPath();
508
+
509
+ ctx.arc(pt.x, pt.y, 5, 0, Math.PI * 2, false);
510
+
511
+ ctx.closePath();
512
+
513
+ ctx.fillStyle = color;
514
+
515
+ ctx.fill();
516
+
517
+ }
518
+
519
+
520
+
521
+
522
+
523
+ function draw(h) {
524
+
525
+
526
+
527
+ ctx.beginPath();
528
+
529
+ ctx.moveTo(h.s.x, h.s.y);
530
+
531
+ ctx.bezierCurveTo(h.c1.x, h.c1.y, h.c2.x, h.c2.y, h.e.x, h.e.y);
532
+
533
+ ctx.strokeStyle = "orange";
534
+
535
+ ctx.lineWidth = 3;
536
+
537
+ ctx.stroke();
538
+
539
+
540
+
541
+ if (showControls) {
542
+
543
+ dot(h.s, "green");
544
+
545
+ dot(h.c1, "red");
546
+
547
+ dot(h.c2, "blue");
548
+
549
+ dot(h.e, "purple");
550
+
551
+ ctx.beginPath();
552
+
553
+ ctx.rect(h.left, h.top - YY, (h.right - h.left), h.length * (1 - C1Y) + YY)
554
+
555
+ ctx.lineWidth = 1;
556
+
557
+ ctx.strokeStyle = "lightgray";
558
+
559
+ ctx.stroke();
560
+
561
+ }
562
+
563
+
564
+
565
+ }
566
+
567
+ ```