-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
156 lines (133 loc) · 4.98 KB
/
index.html
File metadata and controls
156 lines (133 loc) · 4.98 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@rolemodel/image-diff Demo</title>
<style>
body {
width: 400px;
display: flex;
flex-direction: column;
gap: 16px;
}
canvas {
border: 1px solid #000000;
cursor: crosshair;
}
.canvas-label {
font-weight: bold;
margin-bottom: 8px;
}
.canvas-options {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 8px;
margin-bottom: 8px;
}
</style>
</head>
<body>
<div class="canvas-option">
<label for="drawing-color">Drawing Color:</label>
<input type="color" id="drawing-color" value="#ff0000">
</div>
<div class="canvas-wrapper">
<div><span class="canvas-label">Original</span></div>
<canvas id="original"></canvas>
</div>
<div class="canvas-wrapper">
<div>
<span class="canvas-label">Modified</span>
<div class="canvas-options">
<button type="button" id="copy" class="copy-button">Copy from original</button>
</div>
</div>
<canvas id="modified"></canvas>
</div>
<div class="canvas-wrapper">
<div>
<span class="canvas-label">Result</span>
<div class="canvas-options">
<div class="canvas-option">
<label for="diff-threshold">Diff Threshold:</label>
<input type="range" id="diff-threshold" min="0" max="1" step="0.01" value="0.2">
</div>
<div class="canvas-option">
<label for="background-alpha">Background Alpha:</label>
<input type="range" id="background-alpha" min="0" max="1" step="0.01" value="1.0">
</div>
<div class="canvas-option">
<label for="addition-color">Addition Color:</label>
<input type="color" id="addition-color" value="#ff0000">
</div>
<div class="canvas-option">
<label for="deletion-color">Deletion Color:</label>
<input type="color" id="deletion-color" value="#0000ff">
</div>
</div>
</div>
<canvas id="result"></canvas>
</div>
<script type="module">
import ImageDiff from './src/ImageDiff.js'
const originalCanvas = document.getElementById('original')
const originalContext = originalCanvas.getContext('2d')
const modifiedCanvas = document.getElementById('modified')
const modifiedContext = modifiedCanvas.getContext('2d')
const resultCanvas = document.getElementById('result')
const resultContext = resultCanvas.getContext('2d')
const copyButton = document.getElementById('copy')
const drawingColorInput = document.getElementById('drawing-color')
const diffThresholdInput = document.getElementById('diff-threshold')
const backgroundAlphaInput = document.getElementById('background-alpha')
const additionColorInput = document.getElementById('addition-color')
const deletionColorInput = document.getElementById('deletion-color')
const diff = new ImageDiff(originalCanvas, modifiedCanvas)
function diffOptions() {
const additionColor = additionColorInput.value
const r = parseInt(additionColor.slice(1, 3), 16) / 255
const g = parseInt(additionColor.slice(3, 5), 16) / 255
const b = parseInt(additionColor.slice(5, 7), 16) / 255
const deletionColor = deletionColorInput.value
const dr = parseInt(deletionColor.slice(1, 3), 16) / 255
const dg = parseInt(deletionColor.slice(3, 5), 16) / 255
const db = parseInt(deletionColor.slice(5, 7), 16) / 255
return {
additionColor: { r, g, b },
deletionColor: { r: dr, g: dg, b: db },
threshold: parseFloat(diffThresholdInput.value),
backgroundAlpha: parseFloat(backgroundAlphaInput.value)
}
}
function updateDiff() {
const diffCanvas = diff.update(diffOptions())
resultContext.clearRect(0, 0, resultCanvas.width, resultCanvas.height)
resultContext.drawImage(diffCanvas, 0, 0)
}
function addCanvasEventListeners(canvasContext) {
canvasContext.canvas.addEventListener('pointermove', event => {
if (event.buttons !== 1) return
const rect = canvasContext.canvas.getBoundingClientRect()
const x = event.clientX - rect.left
const y = event.clientY - rect.top
const ctx = canvasContext
ctx.fillStyle = drawingColorInput.value
ctx.fillRect(x - 5, y - 5, 10, 10)
updateDiff()
})
}
addCanvasEventListeners(originalContext)
addCanvasEventListeners(modifiedContext)
copyButton.addEventListener('click', () => {
modifiedContext.clearRect(0, 0, modifiedCanvas.width, modifiedCanvas.height)
modifiedContext.drawImage(originalCanvas, 0, 0)
updateDiff()
})
diffThresholdInput.addEventListener('input', updateDiff)
backgroundAlphaInput.addEventListener('input', updateDiff)
diffColorInput.addEventListener('input', updateDiff)
</script>
</body>
</html>