-
Notifications
You must be signed in to change notification settings - Fork 1
/
circle-zoom.html
153 lines (130 loc) · 3.83 KB
/
circle-zoom.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
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
<!DOCTYPE html>
<html>
<head>
<title>scroll</title>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<style type="text/css">
body{
margin: 0;
}
#container{
height: 100vh;
width: 100vw;
}
</style>
</head>
<body>
<div id="container">
<canvas id="canvas"></canvas>
</div>
<script type="text/javascript" src="./lib/weRender.min.js"></script>
<script type="text/javascript" src="./lib/weScroll.min.js"></script>
<script type="text/javascript">
var WeStage = weRender.WeStage;
var WeCanvas = weRender.WeCanvas;
var stageEl = document.querySelector("#canvas");
var wrapper = stageEl.parentNode;
var ratio = window.devicePixelRatio || 1;
var stage = new WeStage(stageEl, {
ratio: ratio
});
var canvasWidth = wrapper.clientWidth;
var canvasHeight = wrapper.clientHeight;
stage.setSize(canvasWidth * ratio, canvasHeight * ratio);
stage.setStyle(canvasWidth + "px", canvasHeight + "px");
var zoomed = false;
var positionX = positionY = null;
stageEl.addEventListener("tap", function(e){
var pageX = e.pageX;
var pageY = e.pageY;
var offset = scroller.wrapperOffset;
var left = offset.left;
var top = offset.top;
var pointX = pageX + left - scroller.x;
var pointY = pageY + top - scroller.y;
var scale = scroller.scale;
positionX = pointX / scale;
positionY = pointY / scale;
if (zoomed) return
requestAnimationFrame(function(){
scroller.scrollTo(positionX, positionY);
});
window.setTimeout(function(){
scroller.zoom(2, positionX, positionY);
zoomed = true;
}, 360);
}, false);
var circleWidth = circleHeight = 30;
var cache = {};
function getCircleCanvas(scale, color){
var scale = (scale || 1) * ratio;
var color = color || "#3BACD9";
var key = "" + scale + ":" + color;
var canvas = cache[key];
if (canvas) return canvas;
var circle = new WeCanvas({
width: circleWidth * scale ,
height: circleHeight * scale,
})
.scale(scale, scale)
.beginPath()
.fillStyle(color)
.arc(15, 15, 10, 0, 2 * Math.PI)
.fill()
.draw()
cache[key] = circle.canvas
return circle.canvas
}
function isTapped(x, y, pointX, pointY){
return (pointX > x && pointX < x + circleWidth) &&
(pointY > y && pointY < y + circleHeight)
}
function isVisible(offsetX, offsetY, x, y, canvas){
var width = canvas.width;
var height = canvas.height;
return ( x + width + offsetX > 0 && x + offsetX < canvasWidth) &&
(y + height + offsetY > 0 && y + offsetY < canvasHeight)
}
var contentWidth = contentHeight = 800;
var maxRow = Math.floor(contentHeight / circleHeight);
var maxCol = Math.floor(contentWidth / circleWidth);
var render = function(offsetX, offsetY, scale){
stage.translate(offsetX, offsetY, true);
var canvas = getCircleCanvas(scale);
for (var row = 0; row < maxRow; row ++){
for (var col = 0; col < maxCol; col ++) {
var x = (col * circleWidth + 10) * scale,
y = (row * circleHeight + 10) * scale;
// if visible
if (isVisible(offsetX, offsetY, x, y, canvas)) {
var tapped = isTapped(x / scale, y / scale, positionX, positionY)
stage.addChild({
canvas: tapped ? getCircleCanvas(scale, "red") : canvas,
x: x,
y: y
});
}
}
}
stage.update();
}
var zoomMin = canvasWidth / contentWidth;
var scroller = new WeScroll(wrapper, {
tap: true,
disablePointer: true,
disableTouch: false,
disableMouse: false,
zoom: true,
zoomMin: zoomMin,
startZoom: zoomMin,
zoomMax: 2,
contentWidth: contentWidth,
contentHeight: contentHeight,
render: render,
});
setTimeout(function(){
scroller.zoom(0.8)
}, 300)
</script>
</body>
</html>