-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
134 lines (125 loc) · 3.91 KB
/
index.js
File metadata and controls
134 lines (125 loc) · 3.91 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
import {
DESIGN_WIDTH, DESIGN_SIZE_RATIO
} from './utils/constants.js'
import {checkDesign} from './core/prepare.js'
import {extractStuff} from './core/stuff.js'
import {extractStructure} from './core/structure.js'
import {addStylesToStructure} from './core/style.js'
import {generateCode} from './core/code.js'
/* Config */
const DESIGN_SRC = './design/kbl-2.png'
const WECHAT_HEADER = window.WECHAT_HEADER = true
/* Constants */
const canvas = document.getElementById('design')
const ctx = window.ctx = canvas.getContext('2d')
const processCanvas =
window.processCanvas = document.getElementById('process')
const processCtx =
window.processCtx = processCanvas.getContext('2d')
/* Init */
loadDesign((imageData, designSizeRatio) => {
imageData = checkDesign(imageData)
initAssistance(imageData, designSizeRatio)
const detailedStuff = extractStuff(imageData)
console.info('DETAILED_STUFF', detailedStuff)
let structure = extractStructure(detailedStuff, imageData)
structure = addStylesToStructure(structure, detailedStuff)
console.info('STRUCTURE', structure)
const completeCode = generateCode(structure, detailedStuff)
})
/* Functions */
function loadDesign (callback) {
const image = new Image()
image.onload = function () {
const {width, height} = this
if (!Object.values(DESIGN_WIDTH).includes(width)) {
console.error(`Unexpected design width: ${width}`)
return
}
const designSizeRatio = DESIGN_SIZE_RATIO[width]
canvas.width = processCanvas.width = width
canvas.height = processCanvas.height = height
canvas.style.width =
processCanvas.style.width = `${width / designSizeRatio}px`
canvas.style.height =
processCanvas.style.height = `${height / designSizeRatio}px`
ctx.drawImage(this, 0, 0)
const imageData = ctx.getImageData(0, 0, width, height)
callback(imageData, designSizeRatio)
}
image.src = DESIGN_SRC
}
function initAssistance (imageData, designSizeRatio) {
const focusPosition = {
x: null, y: null
}
function focus (x, y) {
function highlight (data, index) {
data[index] = 255
data[index + 1] = 127
data[index + 2] = 0
}
const {width, height} = imageData
x = GeometryUtils.clamp(0, width, x)
y = GeometryUtils.clamp(0, height, y)
const {x: latestX, y: latestY} = focusPosition
if (x === latestX && y === latestY) return
focusPosition.x = x
focusPosition.y = y
const focusImageData = new ImageData(width, height)
const {data} = focusImageData
data.set(imageData.data)
const index = (y * width + x) * 4
const color = {
r: data[index],
g: data[index + 1],
b: data[index + 2]
}
for (let i = width * y; i < width * (y + 1); i++) {
highlight(data, i * 4)
}
for (let i = x; i < width * height; i += width) {
highlight(data, i * 4)
}
ctx.putImageData(focusImageData, 0, 0)
document.getElementById('focusPositionX').innerText = x
document.getElementById('focusPositionY').innerText = y
document.getElementById('focusColorR').innerText = color.r
document.getElementById('focusColorG').innerText = color.g
document.getElementById('focusColorB').innerText = color.b
}
canvas.addEventListener('click', event => {
const {offsetX, offsetY} = event
const x = Math.floor(offsetX * designSizeRatio)
const y = Math.floor(offsetY * designSizeRatio)
focus(x, y)
})
document.addEventListener('keydown', event => {
const {code} = event
const arrowKeys = [
'ArrowLeft', 'ArrowRight',
'ArrowUp', 'ArrowDown'
]
if (
arrowKeys.includes(code) &&
focusPosition.x !== null
) {
let {x, y} = focusPosition
switch (code) {
case 'ArrowLeft':
x--
break
case 'ArrowRight':
x++
break
case 'ArrowUp':
y--
break
case 'ArrowDown':
y++
break
}
focus(x, y)
}
})
}