This commit is contained in:
Devine Lu Linvega 2019-01-10 08:41:41 +12:00
parent 60a8c62927
commit 24a7b76254
3 changed files with 34 additions and 36 deletions

View File

@ -17,7 +17,7 @@ function Cursor () {
} }
this.down = function (e) { this.down = function (e) {
this.pos = this.pos_from_event(e) this.pos = this.atEvent(e)
// Translation // Translation
if (DOTGRID.tool.vertexAt(this.pos)) { if (DOTGRID.tool.vertexAt(this.pos)) {
@ -32,7 +32,7 @@ function Cursor () {
this.last_pos = { x: 0, y: 0 } this.last_pos = { x: 0, y: 0 }
this.move = function (e) { this.move = function (e) {
this.pos = this.pos_from_event(e) this.pos = this.atEvent(e)
// Translation // Translation
if (this.translation) { if (this.translation) {
@ -50,7 +50,7 @@ function Cursor () {
} }
this.up = function (e) { this.up = function (e) {
this.pos = this.pos_from_event(e) this.pos = this.atEvent(e)
if (this.translation && !is_equal(this.translation.from, this.translation.to)) { if (this.translation && !is_equal(this.translation.from, this.translation.to)) {
if (this.translation.layer === true) { DOTGRID.tool.translateLayer(this.translation.from, this.translation.to) } else if (this.translation.copy) { DOTGRID.tool.translateCopy(this.translation.from, this.translation.to) } else if (this.translation.multi) { DOTGRID.tool.translateMulti(this.translation.from, this.translation.to) } else { DOTGRID.tool.translate(this.translation.from, this.translation.to) } if (this.translation.layer === true) { DOTGRID.tool.translateLayer(this.translation.from, this.translation.to) } else if (this.translation.copy) { DOTGRID.tool.translateCopy(this.translation.from, this.translation.to) } else if (this.translation.multi) { DOTGRID.tool.translateMulti(this.translation.from, this.translation.to) } else { DOTGRID.tool.translate(this.translation.from, this.translation.to) }
@ -67,7 +67,7 @@ function Cursor () {
} }
this.alt = function (e) { this.alt = function (e) {
this.pos = this.pos_from_event(e) this.pos = this.atEvent(e)
DOTGRID.tool.removeSegmentsAt(this.pos) DOTGRID.tool.removeSegmentsAt(this.pos)
e.preventDefault() e.preventDefault()
@ -77,18 +77,18 @@ function Cursor () {
// Position Mods // Position Mods
this.pos_from_event = function (e) { this.atEvent = function (e) {
return this.pos_snap(this.pos_relative({ x: e.clientX, y: e.clientY })) return this.snapPos(this.relativePos({ x: e.clientX, y: e.clientY }))
} }
this.pos_relative = function (pos) { this.relativePos = function (pos) {
return { return {
x: pos.x - DOTGRID.renderer.el.offsetLeft, x: pos.x - DOTGRID.renderer.el.offsetLeft,
y: pos.y - DOTGRID.renderer.el.offsetTop y: pos.y - DOTGRID.renderer.el.offsetTop
} }
} }
this.pos_snap = function (pos) { this.snapPos = function (pos) {
return { return {
x: clamp(step(pos.x, 15), 15, DOTGRID.tool.settings.size.width), x: clamp(step(pos.x, 15), 15, DOTGRID.tool.settings.size.width),
y: clamp(step(pos.y, 15), 15, DOTGRID.tool.settings.size.height + 15) y: clamp(step(pos.y, 15), 15, DOTGRID.tool.settings.size.height + 15)

View File

@ -52,7 +52,7 @@ function Dotgrid (width, height) {
// File // File
this.new = function () { this.new = function () {
this.set_zoom(1.0) this.setZoom(1.0)
this.setSize({ width: 300, height: 300 }) this.setSize({ width: 300, height: 300 })
this.history.push(this.tool.layers) this.history.push(this.tool.layers)
this.clear() this.clear()
@ -72,32 +72,6 @@ function Dotgrid (width, height) {
}) })
} }
function grab (base64, name) {
const link = document.createElement('a')
link.setAttribute('href', base64)
link.setAttribute('download', name)
link.dispatchEvent(new MouseEvent(`click`, { bubbles: true, cancelable: true, view: window }))
}
this.save = function () {
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to save'); return }
this.manager.toGRID(grab)
}
this.export = function () {
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to export'); return }
this.manager.toSVG(grab)
}
this.render = function () {
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to render'); return }
const size = { width: DOTGRID.tool.settings.size.width * 2, height: DOTGRID.tool.settings.size.height * 2 }
this.manager.toPNG(size, grab)
}
// Basics // Basics
this.getSize = function () { this.getSize = function () {
@ -125,7 +99,7 @@ function Dotgrid (width, height) {
this.renderer.update() this.renderer.update()
} }
this.set_zoom = function (scale) { this.setZoom = function (scale) {
this.setSize({ width: this.tool.settings.size.width, height: this.tool.settings.size.height }, true, scale) this.setSize({ width: this.tool.settings.size.width, height: this.tool.settings.size.height }, true, scale)
try { try {

View File

@ -79,4 +79,28 @@ function Manager (dotgrid) {
const file = new Blob([text], { type: 'text/plain' }) const file = new Blob([text], { type: 'text/plain' })
callback(URL.createObjectURL(file), 'export.grid') callback(URL.createObjectURL(file), 'export.grid')
} }
// Helpers
this.save = function () {
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to save'); return }
this.toGRID(grab)
}
this.export = function () {
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to export'); return }
this.toSVG(grab)
}
this.render = function () {
if (DOTGRID.tool.length() < 1) { console.warn('Nothing to render'); return }
this.toPNG({ width: DOTGRID.tool.settings.size.width * 2, height: DOTGRID.tool.settings.size.height * 2 }, grab)
}
function grab (base64, name) {
const link = document.createElement('a')
link.setAttribute('href', base64)
link.setAttribute('download', name)
link.dispatchEvent(new MouseEvent(`click`, { bubbles: true, cancelable: true, view: window }))
}
} }