-
Notifications
You must be signed in to change notification settings - Fork 0
/
kl3v.html
361 lines (330 loc) · 16.2 KB
/
kl3v.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<html>
<head>
<meta charset="utf-8" />
<title>Keyboard Layout 3D Viewer</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/101/three.min.js"></script>
<script src="js/OrbitControls.js"></script>
<script src="js/STLLoader.js"></script>
<script src="js/TDSLoader.js"></script>
<script src="js/KLEparser.js"></script>
<script src="https://unpkg.com/vue-swatches"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.4/css/bulma.min.css">
<link rel="stylesheet" href="https://unpkg.com/vue-swatches/dist/vue-swatches.css">
</head>
<body>
<div class="container">
<section class="section">
<h1 class="title">Keyboard Layout 3D Viewer</h1>
<div id="app">
<div class="field">
<label class="label">Keyboard Layout Editor Raw data (Raw data is now supported!)</label>
<div class="control">
<textarea v-model="rawdata" placeholder="Raw data" class="textarea"></textarea>
</div>
</div>
<div class="field is-grouped">
<div class="control">
<label class="label">
PCB color
</label>
<div class="control">
<swatches v-model="pcbcolor" :colors="colors" shapes="circles" show-fallback popover-to="left"></swatches>
</div>
</div>
<div class="control">
<label class="label">
Keycap color
</label>
<div class="control">
<swatches v-model="keycolor" colors="material-dark" shapes="circles" show-fallback popover-to="left"></swatches>
</div>
</div>
<div class="control">
<label class="label">
Legend color
</label>
<div class="control">
<swatches v-model="legendcolor" colors="basic" shapes="circles" show-fallback popover-to="left"></swatches>
</div>
</div>
<div class="control">
<label class="label">Keycap</label>
<div class="select">
<select v-model="selkeycap">
<option>Original</option>
<option>DSA</option>
<option>XDA</option>
<option>SA R3</option>
<!-- <option>Cherry R3</option> -->
<option value="Choc">Choc</option>
</select>
</div>
</div>
<div class="control">
<label class="radio">
<input type="radio" value="perspective" v-model="camera">
Perspective camera
</label>
<label class="radio">
<input type="radio" value="ortographic" v-model="camera">
Orthographic camera
</label>
</div>
<div class="control">
<button id="renderBtn" v-on:click="renderKB" class="button is-dark">Render</button>
</div>
</div>
<div id="container"></div>
</div>
</section>
<article class="message">
<div class="message-body">
<div class="content">
<p>Supported keycap profiles</p>
<ul>
<li>SA R3</li>
<!-- <li>SA R1, R2, R3, R4, SPACE</li> -->
<!-- <li>Cherry R1, R2, R3, R4, SPACE</li> -->
<li>DSA</li>
<li>XDA</li>
<li>Kailh Chocolate "Choc"</li>
</ul>
</div>
</div>
</article>
<footer class="footer">
<div class="content has-text-centered">
<p>
<strong>Keyboard Layout 3D Viewer</strong> by <a href="https://github.com/eswai/">eswai</a>. The source code is licensed
<a href="http://opensource.org/licenses/mit-license.php">MIT</a>.
<!-- SA and Cherry keycaps (c) <a href="https://github.com/hineybush">Josh Hinnebusch.</a> -->
SA keycaps (c) <a href="https://github.com/hineybush">Josh Hinnebusch.</a>
Cherry MX switch (c) <a href="https://grabcad.com/library/cherry-mx-1">Mirko Tadic.</a>
</p>
</div>
</footer>
</div>
</body>
<script type="text/javascript">
const width = 960
const height = 540
var app = new Vue({
el: '#app',
components: {Swatches: window['vue-swatches']},
data: {
keycaps: {},
selkeycap: 'Original',
mxswitch: false,
chocswitch: false,
pcb: false,
pcbcolor: '#EEEEEE',
keycolor: '',
legendcolor: '',
colors: ['#EEEEEE', '#6A0888', '#B40404', '#151515', '#088A08', '#08298A', "#AEB404"],
camera: "perspective",
curcamera: "perspective",
renderer: false,
scene: false,
cameraobj: false,
tobj: [],
rid: -1,
// rawdata: "[{x:3.5,c:\"#2840dd\",p:\"SA R1\",a:7},\"3\",{x:10.5},\"\"],[{y:-0.875,x:2.5},\"\",{x:1},\"\",{x:8.5},\"\",{x:1},\"\"],[{y:-0.875,x:5.5},\"\",\"\",{x:4.5},\"\",\"\"],[{y:-0.875,w:1.5},\"\",\"\",{x:14.5},\"\",{w:1.5},\"\"],[{y:-0.375,x:3.5,c:\"#5c6cc3\",p:\"SA R2\"},\"\",{x:10.5},\"\"],[{y:-0.875,x:2.5},\"\",{x:1},\"\",{x:8.5},\"\",{x:1},\"\"],[{y:-0.875,x:5.5},\"\",{p:\"SA R3\",h:1.5},\"\",{x:4.5,h:1.5},\"\",{p:\"SA R2\"},\"\"],[{y:-0.875,w:1.5},\"\",\"\",{x:14.5},\"\",{w:1.5},\"\"],[{y:-0.375,x:3.5,c:\"#4284c8\"},\"\",{x:10.5},\"\"],[{y:-0.875,x:2.5,p:\"SA R3\"},\"\",{x:1},\"\",{x:8.5},\"\",{x:1},\"\"],[{y:-0.875,x:5.5},\"\",{x:6.5},\"\"],[{y:-0.875,w:1.5},\"\",\"\",{x:14.5},\"\",{w:1.5},\"\"],[{y:-0.625,x:6.5,c:\"#65b7ec\",h:1.5},\"\",{x:4.5,h:1.5},\"\"],[{y:-0.75,x:3.5,p:\"SA R4\"},\"\",{x:10.5},\"\"],[{y:-0.875,x:2.5},\"\",{x:1},\"\",{x:8.5},\"\",{x:1},\"\"],[{y:-0.875,x:5.5},\"\",{x:6.5},\"\"],[{y:-0.875,w:1.5},\"\",\"\",{x:14.5},\"\",{w:1.5},\"\"],[{y:-0.375,x:3.5,c:\"#81d7e2\",p:\"XDA\"},\"\",{x:10.5},\"\"],[{y:-0.875,x:2.5},\"\",{x:1},\"\",{x:8.5},\"\",{x:1},\"\"],[{y:-0.75,x:0.5},\"\",\"\",{x:14.5},\"\",\"\"],[{r:30,rx:6.5,ry:4.25,y:-1,x:1,c:\"#4e4c4c\",p:\"DSA\"},\"\",\"\"],[{h:2},\"\",{h:2},\"\",\"\"],[{x:2},\"\"],[{r:-30,rx:13,y:-1,x:-3,c:\"#483527\",p:\"Choc\"},\"\",\"\"],[{x:-3},\"\",{h:2},\"\",{h:2},\"\"],[{x:-3},\"\"]"
// rawdata: "[{c:\"#3d3d3d\",t:\"#ffffff\",p:\"DSA\",a:7},\"Tab\",{c:\"#919191\"},\"Q\",\"W\",\"E\",\"R\",\"T\",\"-\",\"+\",\"Y\",\"U\",\"I\",\"O\",\"P\",{c:\"#3d3d3d\"},\"Back Space\"],[\"Ctrl\",{c:\"#919191\"},\"A\",\"S\",\"D\",\"F\",\"G\",\"[\",\"]\",\"H\",\"J\",\"K\",\"L\",\";\",{c:\"#3d3d3d\"},\"Ctrl\"],[\"Shift\",{c:\"#919191\"},\"Z\",\"X\",\"C\",\"V\",\"B\",\"`\",\"'\",\"N\",\"M\",\",\",\".\",\"/\",{c:\"#3d3d3d\"},\"Alt\"],[{c:\"#c2b900\",t:\"#8a8a8a\"},\"2ND\",{c:\"#3d3d3d\",t:\"#ffffff\"},\"\",\"\",\"\",{x:6,c:\"#086e6e\"},\"←\",\"↓\",\"↑\",\"→\"],[{y:-0.75,x:4,c:\"#3d3d3d\",t:\"#ffe600\"},\"LOWER\",{t:\"#ffffff\"},\"Shift\",{c:\"#919191\"},\"\",{c:\"#086e6e\"},\"Enter\",{c:\"#3d3d3d\"},\"Shift\",{t:\"#ffe600\"},\"RAISE\"]"
// rawdata : "[{c:\"#575757\",t:\"#ffffff\",p:\"DSA\",w:1.25},\"Tab\",{c:\"#e3e3e3\",t:\"#000000\",f:6},\"Q\",\"W\",\"E\",\"R\",\"T\",\"Y\",\"U\",\"I\",\"O\",\"P\",{c:\"#575757\",t:\"#ffffff\",f:3,w:1.75},\"Backspace\"],[{w:1.25},\"Menu\",{c:\"#e3e3e3\",t:\"#000000\",f:6},\"A\",\"S\",\"D\",\"F\",\"G\",\"H\",\"J\",\"K\",\"L\",\":\\n;\",{c:\"#575757\",t:\"#ffffff\",f:3,w:1.75},\"Enter\"],[{w:1.25},\"Shift\",{c:\"#e3e3e3\",t:\"#000000\",f:6},\"Z\",\"X\",\"C\",\"V\",\"B\",\"N\",\"M\",\"<\\n,\",\">\\n.\",\"?\\n/\",{c:\"#ab0202\",t:\"#ffffff\",f:3},\"↑\",{c:\"#575757\"},\"Esc\"],[{w:1.25},\"Ctrl\",{w:1.25},\"Data\",{w:1.5},\"FN\",\"META\",{w:1.25},\"Alt\",{w:1.25},\"Ctrl\",\"SUPER\",{w:1.5},\"FN\",{x:0.25,c:\"#ab0202\",t:\"#e3e3e3\"},\"←\",\"↓\",\"→\"]"
rawdata: "[{r:10,rx:1,y:-0.1,x:2,c:\"#e3e3e3\"},\"E\"],[{y:-0.65,x:1},\"W\",{x:1},\"R\"],[{y:-0.75},\"Q\"],[{y:-0.9,x:4},\"T\"],[{y:-0.7,x:2},\"D\"],[{y:-0.65,x:1},\"S\",{x:1},\"F\"],[{y:-0.75},\"A\"],[{y:-0.9,x:4},\"G\"],[{y:-0.7,x:2},\"C\"],[{y:-0.65,x:1},\"X\",{x:1},\"V\"],[{y:-0.75},\"Z\"],[{y:-0.9,x:4},\"B\"],[{y:-0.75,x:5,c:\"#575757\",t:\"#ffffff\",h:1.5},\"Ctrl\"],[{y:-0.95,x:2},\"super\"],[{y:-0.65,x:1},\"Tab\",{x:1},\"Shift\"],[{y:-0.75,c:\"#ab0202\",t:\"#e3e3e3\"},\"Esc\"],[{y:-0.9,x:4,c:\"#575757\",t:\"#ffffff\"},\"Bksp\"],[{r:-10,rx:7,ry:0.965,y:-0.2,x:2,c:\"#e3e3e3\",t:\"#000000\"},\"I\"],[{y:-0.65,x:1},\"U\",{x:1},\"O\"],[{y:-0.75,x:4},\"P\"],[{y:-0.9},\"Y\"],[{y:-0.7,x:2},\"K\"],[{y:-0.65,x:1},\"J\",{x:1},\"L\"],[{y:-0.75,x:4},\":\"],[{y:-0.9},\"H\"],[{y:-0.7,x:2},\"<\"],[{y:-0.65,x:1},\"M\",{x:1},\">\"],[{y:-0.75,x:4},\"?\"],[{y:-0.9},\"N\"],[{y:-0.75,x:-1,c:\"#575757\",t:\"#ffffff\",h:1.5},\"Alt\"],[{y:-0.95,x:2,c:\"#e3e3e3\",t:\"#000000\"},\"_\"],[{y:-0.65,x:1,c:\"#575757\",t:\"#ffffff\"},\"fn\",{x:1,c:\"#e3e3e3\",t:\"#000000\"},\"\\\"\"],[{y:-0.75,x:4,c:\"#ab0202\",t:\"#e3e3e3\"},\"Enter\"],[{y:-0.9,c:\"#e3e3e3\",t:\"#000000\"},\"Space\"]"
},
created: function() {
console.log("Loading keycaps...")
var tloader = new THREE.TDSLoader()
tloader.setPath('./models/')
this.keycaps = {
'DSA': 'dsa.3ds',
'XDA': 'xda.3ds',
// 'SA R1': 'sa-1u-r1.stl',
// 'SA R2': 'sa-1u-r2.stl',
'SA R3': 'sa-1u-r3.3ds',
// 'SA R4': 'sa-1u-r4.stl',
// 'SA SPACE': 'sa-7u-space.stl',
'Choc': 'choc.3ds',
// 'Cherry R1': 'cherry-r1.stl',
// 'Cherry R2': 'cherry-r2.stl',
// 'Cherry R3': 'cherry-r3.stl',
// 'Cherry R4': 'cherry-r4.stl',
// 'Cherry SPACE': 'cherry-space.stl'
}
Object.keys(this.keycaps).forEach(kn => {
tloader.load(this.keycaps[kn], obj => {
obj.traverse(function ( child ) {
if ( child instanceof THREE.Mesh ) {
child.geometry.computeBoundingBox()
}
})
this.keycaps[kn] = obj
console.log(kn + ' loaded')
})
})
var sloader = new THREE.STLLoader()
sloader.setPath('./models/')
sloader.load('pcb.stl', stl => {
this.pcb = stl
})
sloader.load('mx-switch.stl', stl => {
this.mxswitch = stl
})
sloader.load('choc-switch.stl', stl => {
this.chocswitch = stl
})
},
methods: {
makeCamera: function(ctype) {
var camera
if (ctype == "perspective") {
camera = new THREE.PerspectiveCamera( 45, width / height, 1, 2000 )
} else {
camera = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, 1, 2000 )
}
this.cameraobj = camera
camera.position.set( 0, 600, 0 )
return camera
},
renderKB: function() {
var that = this
var container = document.getElementById( 'container' )
var renderer = that.renderer
var scene = that.scene
var camera = that.cameraobj
if (that.rid >= 0) {
cancelAnimationFrame(that.rid)
}
while (that.tobj.length > 0) {
scene.remove(that.tobj.shift())
}
if (renderer == false) {
renderer = new THREE.WebGLRenderer( { antialias: true } )
that.renderer = renderer
renderer.setPixelRatio( window.devicePixelRatio )
renderer.setSize( width, height )
renderer.gammaOutput = true
renderer.gammaFactor = 2.2
container.appendChild( renderer.domElement )
camera = this.makeCamera(that.camera)
var controls = new THREE.OrbitControls( camera, renderer.domElement )
controls.target.set( 0, 0.5, 0 )
scene = new THREE.Scene()
that.scene = scene
scene.background = new THREE.Color( 0xf0f0f0 )
scene.add( new THREE.AmbientLight( 0x404040 ) )
pointLight = new THREE.PointLight( 0xffffff, 1 )
pointLight.position.set( 100, 2000, 300 )
scene.add( pointLight )
const plane = new THREE.GridHelper(1900, 100, 0x888888, 0x888888)
plane.position.y = -20
scene.add(plane)
}
if (that.curcamera != that.camera) {
that.curcamera = that.camera
camera = this.makeCamera(that.camera)
var controls = new THREE.OrbitControls( camera, renderer.domElement )
controls.target.set( 0, 0.5, 0 )
}
// parse KLE data and render keyboard
var kb = KLEparser(that.rawdata)
kb.keys.forEach(function(key){
var keycol = that.keycolor == '' ? key.c : that.keycolor
var legcol = that.legendcolor == '' ? key.t : that.legendcolor
// create keycap texture
var canvas = document.createElement("canvas")
canvas.height = 256
canvas.width = 256
var context = canvas.getContext("2d")
var size = 15 * key.f
context.fillStyle = keycol
context.fillRect(0, 0, canvas.width, canvas.height)
context.font = size + "px Arial"
context.textAlign = "center"
context.textBaseline = "middle"
context.fillStyle = legcol
context.fillText(key.mainKey(), canvas.width / 2, canvas.height / 2)
var texture = new THREE.Texture(canvas)
// texture.offset = new THREE.Vector2(0.5, 0.5)
texture.needsUpdate = true
// Keycap model
var geometry
if (that.selkeycap == 'Original') {
geometry = that.keycaps[key.p]
} else {
geometry = that.keycaps[that.selkeycap]
}
if (!geometry) {
geometry = that.keycaps["DSA"]
}
var kc = geometry.clone()
// apply texture on keycap
var material = new THREE.MeshStandardMaterial({
map : texture,
metalness: 0.5,
roughness: 0.6
})
kc.traverse(function(child) {
if (child instanceof THREE.Mesh) {
child.material = material
}
})
that.tobj.push(texture)
// allocate keycap
kc.position.x = key.centerX() * 19
kc.position.z = key.centerY() * 19
kc.scale.x = key.scaleW()
kc.scale.z = key.scaleH()
kc.rotation.y = -key.r
scene.add(kc)
that.tobj.push(kc)
// top plate
var material = new THREE.MeshStandardMaterial({color: that.pcbcolor, metalness: 0.5, roughness: 0.7})
var pcb1 = new THREE.Mesh( that.pcb, material )
var sx = key.w * 19 / 23 + 4 / 23
var sz = key.h * 19 / 23 + 4 / 23
pcb1.position.x = key.centerX() * 19
pcb1.position.y = -7
pcb1.position.z = key.centerY() * 19
pcb1.scale.x = sx
pcb1.scale.z = sz
pcb1.rotation.y = -key.r
scene.add(pcb1)
that.tobj.push(pcb1)
// PCB
var pcb2 = pcb1.clone()
pcb2.position.y = -12
scene.add(pcb2)
that.tobj.push(pcb2)
// bottom plate
var pcb3 = pcb1.clone()
pcb3.position.y = -16
scene.add(pcb3)
that.tobj.push(pcb3)
// key switch
var material = new THREE.MeshStandardMaterial({color: 0x111111, metalness: 0.5, roughness: 0.7})
var sw
if (key.p == "Choc" || that.selkeycap == "Choc") {
sw = new THREE.Mesh( that.chocswitch, material )
} else {
sw = new THREE.Mesh( that.mxswitch, material )
}
sw.position.x = key.centerX() * 19
sw.position.y = -5
sw.position.z = key.centerY() * 19
sw.rotation.y = -key.r
scene.add(sw)
that.tobj.push(sw)
})
tick()
function tick() {
renderer.render(scene, camera)
that.rid = requestAnimationFrame(tick)
}
}
}
})
</script>
</html>