about summary refs log tree commit diff stats
path: root/app/assets/javascripts/svg.js
blob: c103b94682f26e02546eea655affb8c1fa09373d (plain) (blame)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
namespace(function() {

window.createElement = function(type) {
  return document.createElementNS('http://www.w3.org/2000/svg', type)
}

window.drawSymbol = function(params, customMechanics) {
  var svg = createElement('svg')
  svg.setAttribute('viewBox', '0 0 ' + params.width + ' ' + params.height)
  if (!params.x) params.x = 0
  if (!params.y) params.y = 0
  drawSymbolWithSvg(svg, params, customMechanics)
  return svg
}

window.drawSymbolWithSvg = function(svg, params, customMechanics) {
  if (params.type == 'square')        square(svg, params)
  else if (params.type == 'dot')      dot(svg, params)
  else if (params.type == 'gap')      gap(svg, params)
  else if (params.type == 'star')     star(svg, params)
  else if (params.type == 'poly')     poly(svg, params)
  else if (params.type == 'ylop')     ylop(svg, params)
  else if (params.type == 'nega')     nega(svg, params)
  else if (params.type == 'nonce')    { /* Do nothing */ }
  else if (params.type == 'triangle') triangle(svg, params)
  else if (params.type == 'crayon')   crayon(svg, params)
  else if (params.type == 'start')    start(svg, params)
  else if (params.type == 'end')      end(svg, params)
  else if (params.type == 'drag')     drag(svg, params)
  else if (params.type == 'plus')     plus(svg, params)
  else if (params.type == 'minus')    minus(svg, params)
  else if (params.type == 'bridge' && customMechanics) bridge(svg, params)
  else if (params.type == 'arrow'  && customMechanics) arrow(svg, params)
  else if (params.type == 'sizer'  && customMechanics) sizer(svg, params)
  else {console.error('Cannot draw unknown SVG type: ' + params.type)}
}

function square(svg, params) {
  var rect = createElement('rect')
  svg.appendChild(rect)
  rect.setAttribute('width', 28)
  rect.setAttribute('height', 28)
  rect.setAttribute('x', params.width/2-14 + params.x)
  rect.setAttribute('y', params.height/2-14 + params.y)
  rect.setAttribute('rx', 7)
  rect.setAttribute('ry', 7)
  rect.setAttribute('fill', params.color)
  rect.setAttribute('class', params.class)
}

function star(svg, params) {
  var poly = createElement('polygon')
  svg.appendChild(poly)
  var points = [
    '-10.5 -10.5', // Top left
    '-9.5 -4',
    '-15 0',
    '-9.5 4',
    '-10.5 10.5', // Bottom left
    '-4 9.5',
    '0 15',
    '4 9.5',
    '10.5 10.5', // Bottom right
    '9.5 4',
    '15 0',
    '9.5 -4',
    '10.5 -10.5', // Top right
    '4, -9.5',
    '0 -15',
    '-4 -9.5',
  ]
  poly.setAttribute('transform', 'translate(' + (params.width/2 + params.x) + ', ' + (params.height/2 + params.y) + ')')
  poly.setAttribute('points', points.join(', '))
  poly.setAttribute('fill', params.color)
  poly.setAttribute('class', params.class)
}

function poly(svg, params) {
  if (params.polyshape === 0) return
  var size = 10 // Side length of individual squares in the polyomino
  var space = 4 // Gap between squares in the polyomino
  var polyomino = window.polyominoFromPolyshape(params.polyshape)

  var bounds = {'xmin':0, 'xmax':0, 'ymin':0, 'ymax':0}
  for (var i=0; i<polyomino.length; i++) {
    var pos = polyomino[i]
    bounds.xmin = Math.min(bounds.xmin, pos.x)
    bounds.xmax = Math.max(bounds.xmax, pos.x)
    bounds.ymin = Math.min(bounds.ymin, pos.y)
    bounds.ymax = Math.max(bounds.ymax, pos.y)
  }
  var offset = (size+space)/2 // Offset between elements to create the gap
  var centerX = (params.width - size - offset * (bounds.xmax + bounds.xmin)) / 2 + params.x
  var centerY = (params.height - size - offset * (bounds.ymax + bounds.ymin)) / 2 + params.y

  for (var i=0; i<polyomino.length; i++) {
    var pos = polyomino[i]
    if (pos.x%2 !== 0 || pos.y%2 !== 0) continue
    var rect = createElement('rect')
    rect.style.pointerEvents = 'none'
    var transform = 'translate(' + (centerX + pos.x*offset) + ', ' + (centerY + pos.y*offset) + ')'
    if (window.isRotated(params.polyshape)) {
      // -30 degree rotation around the midpoint of the square
      transform = 'rotate(-30, ' + (params.width/2 + params.x) + ', ' + (params.height/2 + params.y) + ') ' + transform
    }
    rect.setAttribute('transform', transform)
    rect.setAttribute('height', size)
    rect.setAttribute('width', size)
    rect.setAttribute('fill', params.color)
    rect.setAttribute('class', params.class)
    svg.appendChild(rect)
  }
}

function ylop(svg, params) {
  if (params.polyshape === 0) return
  var size = 12 // Side length of individual squares in the polyomino
  var space = 2 // Gap between squares in the polyomino
  var polyomino = window.polyominoFromPolyshape(params.polyshape)

  var bounds = {'xmin':0, 'xmax':0, 'ymin':0, 'ymax':0}
  for (var i=0; i<polyomino.length; i++) {
    var pos = polyomino[i]
    bounds.xmin = Math.min(bounds.xmin, pos.x)
    bounds.xmax = Math.max(bounds.xmax, pos.x)
    bounds.ymin = Math.min(bounds.ymin, pos.y)
    bounds.ymax = Math.max(bounds.ymax, pos.y)
  }
  var offset = (size+space)/2 // Offset between elements to create the gap
  var centerX = (params.width - size - offset * (bounds.xmax + bounds.xmin)) / 2 + params.x
  var centerY = (params.height - size - offset * (bounds.ymax + bounds.ymin)) / 2 + params.y

  for (var i=0; i<polyomino.length; i++) {
    var pos = polyomino[i]
    if (pos.x%2 !== 0 || pos.y%2 !== 0) continue
    var poly = createElement('polygon')
    poly.style.pointerEvents = 'none'
    var points = [
      '0 0', '12 0', '12 12', '0 12', '0 3',
      '3 3', '3 9', '9 9', '9 3', '0 3',
    ]
    poly.setAttribute('points', points.join(', '))
    var transform = 'translate(' + (centerX + pos.x*offset) + ', ' + (centerY + pos.y*offset) + ')'
    if (window.isRotated(params.polyshape)) {
      // -30 degree rotation around the midpoint of the square
      transform = 'rotate(-30, ' + (params.width/2 + params.x) + ', ' + (params.height/2 + params.y) + ') ' + transform
    }
    poly.setAttribute('transform', transform)
    poly.setAttribute('fill', params.color)
    poly.setAttribute('class', params.class)
    svg.appendChild(poly)
  }
}

function nega(svg, params) {
  var poly = createElement('polygon')
  svg.appendChild(poly)
  var points = [
    '2.9 -2',
    '2.9 -10.4',
    '-2.9 -10.4',
    '-2.9 -2',
    '-10.2 2.2',
    '-7.3 7.2',
    '0 3',
    '7.3 7.2',
    '10.2 2.2',
  ]
  poly.setAttribute('transform', 'translate(' + (params.width/2 + params.x) + ', ' + (params.height/2 + params.y) + ')')
  poly.setAttribute('points', points.join(', '))
  poly.setAttribute('fill', params.color)
  poly.setAttribute('class', params.class)
}

var triangleDistributions = [
  [],
  [1],
  [2],
  [3],
  [2, 2],
  [2, 3],
  [3, 3],
  [2, 3, 2],
  [3, 2, 3],
  [3, 3, 3]
]

function triangle(svg, params) {
  var distribution = triangleDistributions[params.count]
  var high = distribution.length
  for (var y=0; y<high; y++) {
    var wide = distribution[y]
    for (var x=0; x<wide; x++) {
      var poly = createElement('polygon')
      svg.appendChild(poly)
      var Xcoord = params.x + params.width/2 + 11*(2*x - wide + 1)
      var Ycoord = params.y + params.height/2 + 10*(2*y - high + 1)
      poly.setAttribute('transform', 'translate(' + Xcoord + ', ' + Ycoord + ')')
      poly.setAttribute('points', '0 -8, -8 6, 8 6')
      poly.setAttribute('fill', params.color)
      poly.setAttribute('class', params.class)
    }
  }
}

function crayon(svg, params) {
  var height = params.height

  var poly = createElement('polygon')
  svg.appendChild(poly)
  var points = [
    '0 ' + (height/2),
    (height/2) + ' 0',
    (height/2) + ' ' + height,
  ]
  poly.setAttribute('points', points.join(', '))
  poly.setAttribute('fill', params.color)
  var txt = createElement('text')
  svg.appendChild(txt)
  txt.setAttribute('fill', window.TEXT_COLOR)
  txt.setAttribute('transform', 'translate(' + (height/2 + 10) + ', ' + (height/2 + 6) + ')')
  txt.textContent = params.text
}

function start(svg, params) {
  var circ = createElement('circle')
  svg.appendChild(circ)
  circ.setAttribute('r', 24)
  circ.setAttribute('fill', window.FOREGROUND)
  circ.setAttribute('cx', params.height/2 + params.x)
  circ.setAttribute('cy', params.width/2 + params.y)
}

function end(svg, params) {
  var rect = createElement('rect')
  svg.appendChild(rect)
  rect.setAttribute('width', 24)
  rect.setAttribute('height', 24)
  rect.setAttribute('fill', window.FOREGROUND)
  rect.setAttribute('x', params.height/2 - 12 + params.x)
  rect.setAttribute('y', params.width/2 - 12 + params.y)

  var circ = createElement('circle')
  svg.appendChild(circ)
  circ.setAttribute('r', 12)
  circ.setAttribute('fill', window.FOREGROUND)
  circ.setAttribute('cx', params.height/2 + params.x)
  circ.setAttribute('cy', params.width/2 + params.y)

  if (params.dir === 'left') {
    rect.setAttribute('x', parseInt(rect.getAttribute('x'), 10) - 12)
    circ.setAttribute('cx', parseInt(circ.getAttribute('cx'), 10) - 24)
  } else if (params.dir === 'right') {
    rect.setAttribute('x', parseInt(rect.getAttribute('x'), 10) + 12)
    circ.setAttribute('cx', parseInt(circ.getAttribute('cx'), 10) + 24)
  } else if (params.dir === 'top') {
    rect.setAttribute('y', parseInt(rect.getAttribute('y'), 10) - 12)
    circ.setAttribute('cy', parseInt(circ.getAttribute('cy'), 10) - 24)
  } else if (params.dir === 'bottom') {
    rect.setAttribute('y', parseInt(rect.getAttribute('y'), 10) + 12)
    circ.setAttribute('cy', parseInt(circ.getAttribute('cy'), 10) + 24)
  } else {
    console.error('Endpoint direction not defined!', JSON.stringify(params))
  }
}

function dot(svg, params) {
  var hex = createElement('polygon')
  svg.appendChild(hex)
  hex.setAttribute('points', '5.2 9, 10.4 0, 5.2 -9, -5.2 -9, -10.4 0, -5.2 9')
  hex.setAttribute('transform', 'translate(' + (params.width/2 + params.x) + ', ' + (params.height/2 + params.y) + ')')
  hex.setAttribute('fill', params.color)
  hex.setAttribute('class', params.class)
  hex.setAttribute('stroke', params.stroke)
  hex.setAttribute('stroke-width', params.strokeWidth)
  hex.setAttribute('style', 'pointer-events:none;')
}

function gap(svg, params) {
  if (!params.rot) params.rot = 0
  var centerX = params.height/2 + params.x
  var centerY = params.width/2 + params.y
  var rotate = function(degrees) {return 'rotate(' + degrees + ', ' + centerX + ', ' + centerY + ')'}

  var rect = createElement('rect')
  svg.appendChild(rect)
  rect.setAttribute('width', 32)
  rect.setAttribute('height', 24)
  rect.setAttribute('fill', window.FOREGROUND)
  rect.setAttribute('transform', rotate(90 * params.rot))
  rect.setAttribute('x', centerX - 40)
  rect.setAttribute('y', centerY - 12)
  rect.setAttribute('shape-rendering', 'crispedges')

  var rect = createElement('rect')
  svg.appendChild(rect)
  rect.setAttribute('width', 32)
  rect.setAttribute('height', 24)
  rect.setAttribute('fill', window.FOREGROUND)
  rect.setAttribute('transform', rotate(90 * params.rot))
  rect.setAttribute('x', centerX + 9)
  rect.setAttribute('y', centerY - 12)
  rect.setAttribute('shape-rendering', 'crispedges')
}

function drag(svg, params) {
  if (!params.rot) params.rot = 0

  for (var i=0; i<6; i++) {
    for (var j=0; j<2; j++) {
      var rect = createElement('rect')
      svg.appendChild(rect)
      rect.setAttribute('width', 2)
      rect.setAttribute('height', 2)
      if (params.rot === 0) {
        rect.setAttribute('x', i*4)
        rect.setAttribute('y', j*4)
      } else {
        rect.setAttribute('y', i*4)
        rect.setAttribute('x', j*4)
      }
      rect.setAttribute('fill', window.PAGE_BACKGROUND)
    }
  }
}

function plus(svg, params) {
  var verti = createElement('rect')
  svg.appendChild(verti)
  verti.setAttribute('x', params.width/2 - 1)
  verti.setAttribute('y', 3)
  verti.setAttribute('width', 2)
  verti.setAttribute('height', params.height - 6)
  verti.setAttribute('fill', window.TEXT_COLOR)
  minus(svg, params)
}

function minus(svg, params) {
  var horiz = createElement('rect')
  svg.appendChild(horiz)
  horiz.setAttribute('x', 3)
  horiz.setAttribute('y', params.height/2 - 1)
  horiz.setAttribute('width', params.width - 6)
  horiz.setAttribute('height', 2)
  horiz.setAttribute('fill', window.TEXT_COLOR)
}

function bridge(svg, params) {
  var poly = createElement('polygon')
  svg.appendChild(poly)
  var points = [
    '-10.58 14.56',
    '-17.12 -5.56',
    '0 -18',
    '17.12 -5.56',
    '10.58 14.56',
    '5.29 7.28',
    '8.56 -2.78',
    '0 -9',
    '-8.56 -2.78',
    '-5.29 7.28',
  ]
  poly.setAttribute('transform', 'translate(' + (params.width/2 + params.x) + ', ' + (params.height/2 + params.y) + ')')
  poly.setAttribute('points', points.join(', '))
  poly.setAttribute('fill', params.color)
  poly.setAttribute('class', params.class)
}

function arrow(svg, params) {
  if (!params.rot) params.rot = 0

  var centerX = params.height/2 + params.x
  var centerY = params.width/2 + params.y
  var rotate = function(degrees) {return 'rotate(' + degrees + ', ' + centerX + ', ' + centerY + ')'}

  var rect = createElement('rect')
  svg.appendChild(rect)
  rect.setAttribute('width', 8)
  rect.setAttribute('height', 46)
  rect.setAttribute('fill', params.color)
  rect.setAttribute('class', params.class)
  rect.setAttribute('transform', rotate(45 * params.rot))
  rect.setAttribute('x', centerX - 4)
  rect.setAttribute('y', centerY - 22)

  for (var i=0; i<params.count; i++) {
    var arrowhead = createElement('polygon')
    svg.appendChild(arrowhead)
    var points = [
      '-24 -15',
      '-21.4 -8.6',
      '0 -19',
      '21.4 -8.6',
      '24 -15',
      '0 -27',
    ]
    var transform = rotate(45 * params.rot)
    transform += ' translate(' + (params.width/2 + params.x) + ', ' + (params.height/2 + params.y + i*12) + ')'
    arrowhead.setAttribute('transform', transform)
    arrowhead.setAttribute('points', points.join(', '))
    arrowhead.setAttribute('fill', params.color)
    arrowhead.setAttribute('class', params.class)
  }
}

function sizer(svg, params) {
  var path = createElement('path')
  svg.appendChild(path)
  path.setAttribute('d',
    'M -24 0 ' +
    'a 24 24 0 0 0  24 -24 ' +
    'a 24 24 0 0 0  24  24 ' +
    'a 24 24 0 0 0 -24  24 ' +
    'a 24 24 0 0 0 -24 -24 ' +
    'z'
  )
  path.setAttribute('fill', params.color)
  path.setAttribute('class', params.class)
  path.setAttribute('transform', 'translate(' + (params.width/2 + params.x) + ', ' + (params.height/2 + params.y) + ')')
}

})