-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.js
235 lines (184 loc) · 6.26 KB
/
demo.js
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
function getRandomColor() {
var letters = '0123456789ABCDEF'.split('');
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
function initCanvas(canvasId) {
var c = document.getElementById(canvasId);
c.width = 600;
c.height = 400;
var ctx = c.getContext("2d");
ctx.fillStyle = getRandomColor();
ctx.fillRect(0, 0, c.width, c.height);
for (var i = 0; i < 15; i++) {
ctx.fillStyle = getRandomColor();
ctx.fillRect(getRandomInt(0, c.width), getRandomInt(0, c.height), getRandomInt(10, c.width), getRandomInt(10, c.height));
}
return c;
}
window.onload = function() {
(function() {
var canvas;
var magnifier;
var canvasId = 'demo_canvas';
canvas = initCanvas(canvasId);
magnifier = JyMagnifier({
targetCanvasId: canvasId
});
magnifier.show(true);
canvas.addEventListener('mousemove', doMouseMove, false);
canvas.addEventListener('touchmove', doMouseMove, false);
function doMouseMove(event) {
if (event.type == 'touchmove') {
event.preventDefault();
}
magnifier.bind(event);
}
})();
(function() {
var canvas;
var magnifier;
var canvasId = 'demo_canvas1';
canvas = initCanvas(canvasId);
magnifier = JyMagnifier({
targetCanvasId: canvasId,
width: 300,
height: 300,
radius: 150,
ratio: 5,
sightColor: 'yellow',
sightSize: 20
});
magnifier.show(true);
canvas.addEventListener('mousemove', doMouseMove, false);
canvas.addEventListener('touchmove', doMouseMove, false);
function doMouseenter(event) {
magnifier.show(true);
}
function doMouseMove(event) {
if (event.type == 'touchmove') {
event.preventDefault();
}
magnifier.bind(event);
}
function doMouseleave(event) {
magnifier.show(false);
}
})();
(function() {
var ratio = 3;
var canvas;
var magnifier;
var canvasId = 'demo_canvas2';
canvas = initCanvas(canvasId);
magnifier = JyMagnifier({
targetCanvasId: canvasId,
width: 200,
height: 400,
ratio: ratio,
magnifierDivId: 'canvas2_magnifier',
sightSize: 0
});
canvas.addEventListener('mousemove', doMouseMove, false);
canvas.addEventListener('touchmove', doMouseMove, false);
canvas.addEventListener('mousewheel', doMousewheel, false);
magnifier.show(true);
function doMouseMove(event) {
if (event.type == 'touchmove') {
event.preventDefault();
}
magnifier.bind(event);
}
function doMousewheel(event) {
if (event && event.preventDefault) {
event.preventDefault();
}
var delta = Math.max(-1, Math.min(1, (event.wheelDelta || -event.detail)));
if (delta > 0) {
ratio = ratio + 0.5;
} else {
ratio = ratio - 0.5;
}
if (ratio < 1) {
ratio = 1;
}
//console.log(delta+"->"+ event.wheelDelta+'->'+ event.detail,ratio);
magnifier.setRatio(ratio);
return false;
}
})();
(function() {
var canvas;
var magnifier;
var canvasId = 'demo_canvas3';
var ratio = 5;
canvas = initCanvas(canvasId);
magnifier = JyMagnifier({
targetCanvasId: canvasId,
width: 200,
height: 200,
radius: 0,
ratio: ratio,
sightType: 'cross',
sightColor: 'black',
magnifierDivStyle: 'border:1px solid black;background:#fff;'
});
canvas.addEventListener("mouseenter", doMouseenter, false);
canvas.addEventListener("touchstart", doMouseenter, false);
canvas.addEventListener('mousemove', doMouseMove, false);
canvas.addEventListener('touchmove', doMouseMove, false);
canvas.addEventListener('mouseleave', doMouseleave, false);
canvas.addEventListener('touchend', doMouseleave, false);
canvas.addEventListener('mousewheel', doMousewheel, false);
function doMouseenter(event) {
if (event.type == 'touchstart') {
event.preventDefault();
}
magnifier.show(true);
}
function doMouseMove(event) {
if (event.type == 'touchmove') {
event.preventDefault();
}
magnifier.bind(event, function(magnifierDiv) {
if (event.touches) {
event = event.touches[0];
}
var px = event.pageX;
var py = event.pageY;
magnifierDiv.style.top = py - 200 + 'px';
magnifierDiv.style.left = px - 250 + 'px';
}
);
}
function doMouseleave(event) {
if (event.type == 'touchend') {
event.preventDefault();
}
magnifier.show(false);
}
function doMousewheel(event) {
if (event && event.preventDefault) {
event.preventDefault();
}
var delta = Math.max(-1, Math.min(1, (event.wheelDelta || -event.detail)));
if (delta > 0) {
ratio = ratio + 0.5;
} else {
ratio = ratio - 0.5;
}
if (ratio < 1) {
ratio = 1;
}
//console.log(delta+"->"+ event.wheelDelta+'->'+ event.detail,ratio);
magnifier.setRatio(ratio);
return false;
}
})();
}