-
Notifications
You must be signed in to change notification settings - Fork 17
/
mouse_drag.html
66 lines (58 loc) · 2.03 KB
/
mouse_drag.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
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="css/base.css" />
<script src="https://code.createjs.com/1.0.0/createjs.min.js"></script>
<script>
// 読み込みが終わってから初期化
window.addEventListener("load", init);
function init() {
// 初期化
var stage = new createjs.Stage("myCanvas");
// ボールの半径
var radius = 100;
// ドラッグした場所を保存する変数
var dragPointX;
var dragPointY;
// ボールを作成
var ball = new createjs.Shape();
ball.graphics.beginFill("DarkRed").drawCircle(0, 0, radius);
ball.x = stage.canvas.width / 2; // 画面中央に配置
ball.y = stage.canvas.height / 2;
stage.addChild(ball);
// インタラクティブの設定
ball.addEventListener("mousedown", handleDown);
ball.addEventListener("pressmove", handleMove);
ball.addEventListener("pressup", handleUp);
// ボールを押したときの処理です
function handleDown(event) {
// ドラッグを開始した座標を覚えておく
dragPointX = stage.mouseX - ball.x;
dragPointY = stage.mouseY - ball.y;
// ボールを半透明にする
ball.alpha = 0.5;
}
// ボールを押した状態で動かしたときの処理です
function handleMove(event) {
// ボールはマウス座標に追随する
// ただしドラッグ開始地点との補正をいれておく
ball.x = stage.mouseX - dragPointX;
ball.y = stage.mouseY - dragPointY;
}
// ボールからマウスを離したときの処理です
function handleUp(event) {
// ボールを元の透明度に戻す
ball.alpha = 1.0;
}
// 時間経過
createjs.Ticker.addEventListener("tick", handleTick);
function handleTick() {
stage.update(); // 画面更新
}
}
</script>
</head>
<body>
<canvas id="myCanvas" width="960" height="540"></canvas>
</body>
</html>