pointvec/desktop/sources/scripts/renderer.js

268 lines
8.9 KiB
JavaScript
Raw Normal View History

2018-10-03 19:27:40 -04:00
'use strict'
2019-01-08 22:20:29 -05:00
function Renderer (dotgrid) {
2018-10-03 19:27:40 -04:00
this.el = document.createElement('canvas')
this.el.id = 'guide'
this.el.width = 640
this.el.height = 640
this.el.style.width = '320px'
this.el.style.height = '320px'
2019-01-08 22:01:01 -05:00
this.context = this.el.getContext('2d')
2019-01-08 01:22:14 -05:00
this.showExtras = true
2018-10-03 19:27:40 -04:00
2019-04-21 19:36:12 -04:00
this.scale = 2 // window.devicePixelRatio
2018-10-03 19:27:40 -04:00
this.start = function () {
this.update()
2017-11-21 17:58:07 -05:00
}
2018-08-03 22:36:45 -04:00
2018-10-03 19:27:40 -04:00
this.update = function (force = false) {
2019-01-08 22:20:29 -05:00
dotgrid.manager.update()
2019-01-09 15:35:10 -05:00
let render = new Image()
render.onload = () => {
this.draw(render)
}
render.src = dotgrid.manager.svg64()
}
2019-01-08 22:01:01 -05:00
2019-01-09 15:35:10 -05:00
this.draw = function (render) {
2018-10-03 19:27:40 -04:00
this.clear()
2019-01-08 01:22:14 -05:00
this.drawMirror()
this.drawRulers()
2019-01-08 22:20:29 -05:00
this.drawGrid()
2019-01-09 15:35:10 -05:00
this.drawRender(render) //
2019-01-08 22:01:01 -05:00
this.drawVertices()
2019-01-08 01:22:14 -05:00
this.drawHandles()
this.drawTranslation()
this.drawCursor()
this.drawPreview()
2018-05-09 18:04:02 -04:00
}
2018-10-03 19:27:40 -04:00
this.clear = function () {
2019-01-08 22:20:29 -05:00
this.context.clearRect(0, 0, this.el.width * this.scale, this.el.height * this.scale)
2018-05-09 18:04:02 -04:00
}
2017-11-21 17:58:07 -05:00
2018-10-03 19:27:40 -04:00
this.toggle = function () {
2019-01-08 01:22:14 -05:00
this.showExtras = !this.showExtras
2018-09-11 21:20:31 -04:00
this.update()
2019-01-08 22:20:29 -05:00
dotgrid.interface.update(true)
2018-01-12 15:00:53 -05:00
}
2018-10-03 19:27:40 -04:00
this.resize = function (size) {
2019-01-08 22:49:34 -05:00
const pad = 15
this.el.width = (size.width + pad) * this.scale
this.el.height = (size.height + pad) * this.scale
this.el.style.width = (size.width + pad) + 'px'
this.el.style.height = (size.height + pad) + 'px'
2017-11-21 17:58:07 -05:00
2018-10-03 19:27:40 -04:00
this.update()
2017-11-21 17:58:07 -05:00
}
2019-01-08 22:20:29 -05:00
// Collections
2019-01-08 01:22:14 -05:00
this.drawMirror = function () {
if (!this.showExtras) { return }
2018-12-24 04:26:58 -05:00
2019-01-09 17:54:16 -05:00
if (dotgrid.tool.style().mirror_style === 0 && dotgrid.tool.settings.crest === false) { return }
2018-12-21 17:07:46 -05:00
2019-01-09 22:37:13 -05:00
const middle = { x: dotgrid.tool.settings.size.width + 15, y: dotgrid.tool.settings.size.height + 15 }
2018-12-21 17:07:46 -05:00
2019-01-09 17:54:16 -05:00
if (dotgrid.tool.style().mirror_style === 1 || dotgrid.tool.style().mirror_style === 3 || dotgrid.tool.settings.crest === true) {
2019-01-15 15:12:20 -05:00
this.drawRule({ x: middle.x, y: 15 * this.scale }, { x: middle.x, y: (dotgrid.tool.settings.size.height) * this.scale })
2018-12-21 17:07:46 -05:00
}
2019-01-09 17:54:16 -05:00
if (dotgrid.tool.style().mirror_style === 2 || dotgrid.tool.style().mirror_style === 3 || dotgrid.tool.settings.crest === true) {
2019-01-15 15:12:20 -05:00
this.drawRule({ x: 15 * this.scale, y: middle.y }, { x: (dotgrid.tool.settings.size.width) * this.scale, y: middle.y })
2018-12-21 17:07:46 -05:00
}
}
2019-01-08 01:22:14 -05:00
this.drawHandles = function () {
if (!this.showExtras) { return }
2018-05-11 18:19:24 -04:00
2019-01-08 22:49:34 -05:00
for (const segmentId in dotgrid.tool.layer()) {
const segment = dotgrid.tool.layer()[segmentId]
for (const vertexId in segment.vertices) {
const vertex = segment.vertices[vertexId]
2019-01-08 01:22:14 -05:00
this.drawHandle(vertex)
2018-05-09 17:18:30 -04:00
}
}
2018-05-09 18:04:02 -04:00
}
2018-05-09 17:18:30 -04:00
2019-01-08 01:22:14 -05:00
this.drawVertices = function () {
2019-01-08 22:20:29 -05:00
for (const id in dotgrid.tool.vertices) {
this.drawVertex(dotgrid.tool.vertices[id])
2017-11-09 14:47:06 -05:00
}
2018-05-09 18:04:02 -04:00
}
2018-05-07 19:32:00 -04:00
2019-01-08 22:20:29 -05:00
this.drawGrid = function () {
2019-01-08 01:22:14 -05:00
if (!this.showExtras) { return }
2018-05-10 04:01:31 -04:00
2019-01-09 15:35:10 -05:00
const cursor = { x: parseInt(dotgrid.cursor.pos.x / 15), y: parseInt(dotgrid.cursor.pos.y / 15) }
const markers = dotgrid.getSize().markers
2018-08-02 19:41:37 -04:00
2019-01-09 15:35:10 -05:00
for (let x = markers.w - 1; x >= 0; x--) {
2019-01-09 22:28:04 -05:00
for (let y = markers.h - 1; y >= 0; y--) {
2019-04-21 19:36:12 -04:00
let is_step = x % 4 === 0 && y % 4 === 0
2018-08-02 19:41:37 -04:00
// Color
2019-01-08 22:20:29 -05:00
let color = is_step ? dotgrid.theme.active.b_med : dotgrid.theme.active.b_low
2019-04-21 19:36:12 -04:00
if ((y === 0 || y === markers.h) && cursor.x === x + 1) { color = dotgrid.theme.active.b_high } else if ((x === 0 || x === markers.w - 1) && cursor.y === y + 1) { color = dotgrid.theme.active.b_high } else if (cursor.x === x + 1 && cursor.y === y + 1) { color = dotgrid.theme.active.b_high }
2018-08-02 19:41:37 -04:00
2019-01-08 01:22:14 -05:00
this.drawMarker({
2019-01-09 15:35:10 -05:00
x: parseInt(x * 15) + 15,
y: parseInt(y * 15) + 15
2018-10-03 19:27:40 -04:00
}, is_step ? 2.5 : 1.5, color)
2018-05-09 18:04:02 -04:00
}
}
2017-11-09 14:47:06 -05:00
}
2017-11-12 21:25:29 -05:00
2019-01-08 22:20:29 -05:00
this.drawRulers = function () {
if (!dotgrid.cursor.translation) { return }
const pos = dotgrid.cursor.translation.to
const bottom = (dotgrid.tool.settings.size.height * this.scale)
const right = (dotgrid.tool.settings.size.width * this.scale)
this.drawRule({ x: pos.x * this.scale, y: 0 }, { x: pos.x * this.scale, y: bottom })
this.drawRule({ x: 0, y: pos.y * this.scale }, { x: right, y: pos.y * this.scale })
}
2019-01-08 22:24:58 -05:00
this.drawPreview = function () {
let operation = dotgrid.cursor.operation && dotgrid.cursor.operation.cast ? dotgrid.cursor.operation.cast : null
if (!dotgrid.tool.canCast(operation)) { return }
2019-04-21 19:36:12 -04:00
if (operation === 'close') { return }
2019-01-08 22:24:58 -05:00
let path = new Generator([{ vertices: dotgrid.tool.vertices, type: operation }]).toString({ x: 0, y: 0 }, 2)
let style = {
color: dotgrid.theme.active.f_med,
thickness: 2,
strokeLinecap: 'round',
strokeLinejoin: 'round',
strokeLineDash: [5, 15]
}
this.drawPath(path, style)
}
2019-01-08 22:20:29 -05:00
// Elements
2019-01-08 01:22:14 -05:00
this.drawMarker = function (pos, radius = 1, color) {
2019-01-08 22:24:58 -05:00
this.context.beginPath()
this.context.lineWidth = 2
this.context.arc(pos.x * this.scale, pos.y * this.scale, radius, 0, 2 * Math.PI, false)
this.context.fillStyle = color
this.context.fill()
this.context.closePath()
2018-08-03 22:36:45 -04:00
}
2019-01-08 01:22:14 -05:00
this.drawVertex = function (pos, radius = 5) {
2019-01-08 22:24:58 -05:00
this.context.beginPath()
this.context.lineWidth = 2
this.context.arc((pos.x * this.scale), (pos.y * this.scale), radius, 0, 2 * Math.PI, false)
this.context.fillStyle = dotgrid.theme.active.f_med
this.context.fill()
this.context.closePath()
}
2019-01-08 01:22:14 -05:00
this.drawRule = function (from, to) {
2019-01-08 22:24:58 -05:00
this.context.beginPath()
this.context.moveTo(from.x, from.y)
this.context.lineTo(to.x, to.y)
this.context.lineCap = 'round'
this.context.lineWidth = 3
this.context.strokeStyle = dotgrid.theme.active.b_low
this.context.stroke()
this.context.closePath()
2018-09-25 15:38:38 -04:00
}
2019-01-08 01:22:14 -05:00
this.drawHandle = function (pos, radius = 6) {
2019-01-08 22:24:58 -05:00
this.context.beginPath()
this.context.lineWidth = 3
this.context.lineCap = 'round'
this.context.arc(Math.abs(pos.x * -this.scale), Math.abs(pos.y * this.scale), radius + 3, 0, 2 * Math.PI, false)
this.context.fillStyle = dotgrid.theme.active.f_high
this.context.fill()
this.context.strokeStyle = dotgrid.theme.active.f_high
this.context.stroke()
this.context.closePath()
this.context.beginPath()
this.context.arc((pos.x * this.scale), (pos.y * this.scale), radius, 0, 2 * Math.PI, false)
this.context.fillStyle = dotgrid.theme.active.f_low
this.context.fill()
this.context.closePath()
this.context.beginPath()
this.context.arc((pos.x * this.scale), (pos.y * this.scale), radius - 3, 0, 2 * Math.PI, false)
this.context.fillStyle = dotgrid.theme.active.f_high
this.context.fill()
this.context.closePath()
2018-05-11 18:19:24 -04:00
}
2019-01-08 01:22:14 -05:00
this.drawPath = function (path, style) {
2018-10-03 19:27:40 -04:00
let p = new Path2D(path)
2018-05-09 17:18:30 -04:00
2019-01-08 22:24:58 -05:00
this.context.strokeStyle = style.color
this.context.lineWidth = style.thickness * this.scale
this.context.lineCap = style.strokeLinecap
this.context.lineJoin = style.strokeLinejoin
2018-05-10 03:49:41 -04:00
2019-04-21 19:36:12 -04:00
if (style.fill && style.fill !== 'none') {
2019-01-08 22:24:58 -05:00
this.context.fillStyle = style.color
this.context.fill(p)
2018-05-09 17:18:30 -04:00
}
2018-08-17 16:04:54 -04:00
// Dash
2019-01-08 22:24:58 -05:00
this.context.save()
if (style.strokeLineDash) { this.context.setLineDash(style.strokeLineDash) } else { this.context.setLineDash([]) }
this.context.stroke(p)
this.context.restore()
2018-05-09 17:18:30 -04:00
}
2019-01-08 01:22:14 -05:00
this.drawTranslation = function () {
2019-01-08 22:20:29 -05:00
if (!dotgrid.cursor.translation) { return }
2018-10-03 19:27:40 -04:00
2019-01-08 22:24:58 -05:00
this.context.save()
2018-10-03 19:27:40 -04:00
2019-01-08 22:24:58 -05:00
this.context.beginPath()
this.context.moveTo((dotgrid.cursor.translation.from.x * this.scale), (dotgrid.cursor.translation.from.y * this.scale))
this.context.lineTo((dotgrid.cursor.translation.to.x * this.scale), (dotgrid.cursor.translation.to.y * this.scale))
this.context.lineCap = 'round'
this.context.lineWidth = 5
this.context.strokeStyle = dotgrid.cursor.translation.multi === true ? dotgrid.theme.active.b_inv : dotgrid.cursor.translation.copy === true ? dotgrid.theme.active.f_med : dotgrid.theme.active.f_low
this.context.setLineDash([5, 10])
this.context.stroke()
this.context.closePath()
2018-10-03 19:27:40 -04:00
2019-01-08 22:24:58 -05:00
this.context.setLineDash([])
this.context.restore()
2017-11-12 21:25:29 -05:00
}
2018-05-07 19:32:00 -04:00
2019-01-08 22:20:29 -05:00
this.drawCursor = function (pos = dotgrid.cursor.pos, radius = dotgrid.tool.style().thickness - 1) {
2019-01-08 22:24:58 -05:00
this.context.save()
this.context.beginPath()
this.context.lineWidth = 3
this.context.lineCap = 'round'
this.context.arc(Math.abs(pos.x * -this.scale), Math.abs(pos.y * this.scale), 5, 0, 2 * Math.PI, false)
this.context.strokeStyle = dotgrid.theme.active.background
this.context.stroke()
this.context.closePath()
this.context.beginPath()
this.context.lineWidth = 3
this.context.lineCap = 'round'
this.context.arc(Math.abs(pos.x * -this.scale), Math.abs(pos.y * this.scale), clamp(radius, 5, 100), 0, 2 * Math.PI, false)
this.context.strokeStyle = dotgrid.theme.active.f_med
this.context.stroke()
this.context.closePath()
this.context.restore()
2018-05-10 03:49:41 -04:00
}
2019-01-09 15:35:10 -05:00
this.drawRender = function (render) {
this.context.drawImage(render, 0, 0, this.el.width, this.el.height)
2019-01-08 22:01:01 -05:00
}
2019-04-21 19:36:12 -04:00
function isEqual (a, b) { return a && b && Math.abs(a.x) === Math.abs(b.x) && Math.abs(a.y) === Math.abs(b.y) }
2018-10-03 19:27:40 -04:00
function clamp (v, min, max) { return v < min ? min : v > max ? max : v }
2018-05-09 03:23:06 -04:00
}