-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslider.html
More file actions
193 lines (156 loc) · 6.96 KB
/
slider.html
File metadata and controls
193 lines (156 loc) · 6.96 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,user-scalable=no">
<meta name="description" content="Canvas 2D Context API Primer">
<meta name="keywords" content="HTML,JavaScript">
<meta name="author" content="Raul Roa">
<title>Touch Events</title>
<style type="text/css">
canvas {
position: absolute;
top: 10px;
left: 10px;
background-color: white;
border-radius: 25px;
border: 1px solid #404040;
}
.slider {
position: absolute;
top: 115px;
left: 85px;
width: 152px;
height: 52px;
}
.bar {
position: relative;
top: 30px;
width: 152px;
height: 2px;
background-color: #404040;
}
.knob {
position: relative;
left: 0;
border: 1px solid #404040;
background-color: #c0c0c0;
width: 50px;
height: 50px;
border-radius: 25px;
}
</style>
<script>
window.onload = function () {
// Get the canvas element
//
var canvas = document.getElementById('canvas');
// Get a reference to the drawing context
//
var ctx = canvas.getContext('2d');
// Get a reference to the slider div
//
var slider = document.getElementById("slider");
// Get a reference to the slider knob
//
var knob = document.getElementById("knob");
// Get a reference to the image
//
var image = document.getElementById("image");
var mouseIsDown = 0;
var knobMid = knob.offsetWidth / 2;
var margin = canvas.offsetLeft - 1;
// Binding events
//
slider.addEventListener("touchstart", function(event) {
touchXY(event);
}, false);
slider.addEventListener("touchmove", function(event) {
touchXY(event);
}, false);
slider.addEventListener("mousedown", function(event) {
mouseDown(event);
}, false);
slider.addEventListener("mousemove", function(event) {
mouseXY(event);
}, false);
slider.addEventListener("mouseup", function(event) {
mouseUp(event);
}, false);
// Draw text
//
textInit();
showVal();
function mouseDown(event){
mouseIsDown = 1;
mouseXY(event);
}
function mouseUp(event){
mouseIsDown = 0;
}
function mouseXY(event){
if(mouseIsDown){
var mouseX = event.pageX - slider.offsetLeft;
if (mouseX >= 0 && mouseX <= slider.offsetWidth ) {
setKnob(mouseX);
}
}
}
function touchXY(event){
event.preventDefault();
var touchX = event.touches[0].pageX - slider.offsetLeft;
if(touchX >= 0 && touchX <= slider.offsetWidth){
setKnob(touchX);
}
}
function textInit() {
ctx.fillStyle = "red";
ctx.font = "24pt Helvetica";
ctx.textAlign = "center";
ctx.textBaseline = "bottom";
}
function showVal() {
// value goes from 0 to slider-width minus knob width
//
var sliderVal = knob.offsetLeft;
// Save state
//
ctx.save();
// Clear canvas
//
ctx.clearRect(0, 0, canvas.width, canvas.height);
var scale = 0.25 + sliderVal / 400;
// Scale the image
//
ctx.scale(scale, scale);
ctx.drawImage(image, 0, 0);
// Restore state
//
ctx.restore();
// Draw text
//
ctx.fillText(sliderVal, canvas.width / 2, canvas.height - 5);
setTimeout(showVal, 16);
}
function setKnob(x) {
var knobX = x - knobMid;
knobX = Math.max(knobX, 0);
knobX = Math.min(knobX, slider.offsetWidth - knob.offsetWidth);
if(knob){
knob.style.left = knobX + 'px';
}
}
};
</script>
</head>
<body>
<canvas id="canvas" width="300" height="200" tabindex='0' class="default">
This browser or document mode doesn't support canvas
</canvas>
<div class="slider" id="slider">
<div class="bar"></div>
<div id="knob" class="knob"></div>
</div>
<img id="image" style="display:none" src="butterfly.png" />
</body>
</html>