-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
demo2.html
178 lines (153 loc) · 4.81 KB
/
demo2.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
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
<html>
<head>
<meta charset="utf8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no">
<title>undo-canvas demo 2</title>
<script src="undo-canvas.js"></script>
<script>
document.addEventListener('DOMContentLoaded', () => {
const c = document.getElementById('undoCanvas')
const context = c.getContext('2d')
const width = c.width
const height = c.height
context.lineJoin = 'round'
context.lineWidth = 3
context.strokeStyle = 'blue'
UndoCanvas.enableUndo(context)
const historyNoLabel = document.getElementById('historyNo')
const historySlider = document.getElementById('historySlider')
historySlider.min = 0
historySlider.max = context.currentHistoryNo
const updateControls = () => {
historyNoLabel.value = context.currentHistoryNo
historySlider.max = context.newestHistoryNo
historySlider.value = context.currentHistoryNo
}
historySlider.addEventListener('input', (e) => {
e.preventDefault()
context.currentHistoryNo = historySlider.value
updateControls()
})
const undoButton = document.getElementById('undoButton')
undoButton.addEventListener('click', (e) => {
e.preventDefault()
context.undo()
updateControls()
})
const redoButton = document.getElementById('redoButton')
redoButton.addEventListener('click', (e) => {
e.preventDefault()
context.redo()
updateControls()
})
const undoTagButton = document.getElementById('undoTagButton')
undoTagButton.addEventListener('click', (e) => {
e.preventDefault()
context.undoTag()
updateControls()
})
const redoTagButton = document.getElementById('redoTagButton')
redoTagButton.addEventListener('click', (e) => {
e.preventDefault()
context.redoTag()
updateControls()
})
let drawing = false
let prevMouseX = 0
let prevMouseY = 0
let connectLine = false
const getMouseX = (e) => {
if(typeof e.targetTouches !== 'undefined'){
return e.targetTouches[0].pageX - c.offsetLeft
}
return e.pageX - c.offsetLeft
}
const getMouseY = (e) => {
if(typeof e.targetTouches !== 'undefined'){
return e.targetTouches[0].pageY - c.offsetTop
}
return e.pageY - c.offsetTop
}
const draw = (e) => {
const mouseX = getMouseX(e)
const mouseY = getMouseY(e)
context.beginPath()
if(connectLine){
context.moveTo(prevMouseX, prevMouseY)
}else{
context.moveTo(mouseX, mouseY)
}
context.lineTo(mouseX, mouseY)
context.closePath()
context.stroke()
prevMouseX = mouseX
prevMouseY = mouseY
connectLine = true
updateControls()
}
c.addEventListener('mousedown', (e) => {
e.preventDefault()
context.putTag()
drawing = true
draw(e)
})
c.addEventListener('mousemove', (e) => {
if(drawing){
e.preventDefault()
draw(e)
}
})
c.addEventListener('mouseleave', (e) => {
connectLine = false
})
document.addEventListener('mouseup', (e) => {
if(drawing){
context.putTag()
}
drawing = false
connectLine = false
})
c.addEventListener('touchstart', (e) => {
e.preventDefault()
context.putTag()
drawing = true
draw(e)
})
c.addEventListener('touchmove', (e) => {
if(drawing){
e.preventDefault()
draw(e)
}
})
document.addEventListener('touchend', (e) => {
if(drawing){
context.putTag()
}
drawing = false
connectLine = false
})
document.addEventListener('touchcancel', (e) => {
if(drawing){
context.putTag()
}
drawing = false
connectLine = false
})
updateControls()
})
</script>
</head>
<body>
<h2>Undo/Redo Tags Demo</h2>
Draw a line on mouse drag
<div style="width: 300px; padding: 0 20px; margin: 0; text-align: center;">
<canvas id="undoCanvas" width="300" height="300"></canvas><br />
<button id="undoTagButton">Undo Tag</button>
<button id="undoButton">Undo</button>
<input id="historyNo" type="text" disabled style="width: 40px; text-align: right"/>
<button id="redoButton">Redo</button>
<button id="redoTagButton">Redo Tag</button><br />
<input id="historySlider" type="range" step="1" style="width: 100%;"/>
</div>
</body>
</html>