From cf1ef08db4c0df5ceeba04801cc46c8785152158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Sun, 1 Oct 2017 00:47:50 +0200 Subject: [PATCH] some triangle cleaning, not fixed --- js/lib/colortriangle.js | 72 +++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/js/lib/colortriangle.js b/js/lib/colortriangle.js index 2c4e60d..d3ec922 100644 --- a/js/lib/colortriangle.js +++ b/js/lib/colortriangle.js @@ -54,21 +54,6 @@ function each (obj, fn) { } } -function getOffsets (el) { - let left = 0 - let top = 0 - - while (el !== null) { - console.log(el) - console.log(el.offsetLeft, el.offsetTop) - left += el.offsetLeft - top += el.offsetTop - el = el.offsetParent - } - - return [left, top] -} - module.exports = class ColorTriangle { /**************** * ColorTriangle * @@ -305,12 +290,35 @@ module.exports = class ColorTriangle { // The Element and the DOM inject (parent) { parent.appendChild(this.container) + } + + _getRelativeCoordinates (evt) { + let pos = {} + let offset = {} + let ref + + // FIXME this doesn't really work with scroll + // this.triangle is the canvas element + + ref = this.triangle.offsetParent - // calculate canvas position on page - const offsets = getOffsets(this.triangle) - this.offset = { - x: offsets[0], - y: offsets[1] + pos.x = evt.touches ? evt.touches[0].pageX : evt.pageX + pos.y = evt.touches ? evt.touches[0].pageY : evt.pageY + + offset.left = this.triangle.offsetLeft + offset.top = this.triangle.offsetTop + + while (ref) { + + offset.left += ref.offsetLeft + offset.top += ref.offsetTop + + ref = ref.offsetParent + } + + return { + x: pos.x - offset.left, + y: pos.y - offset.top } } @@ -379,11 +387,14 @@ module.exports = class ColorTriangle { doc.body.addEventListener('mousemove', mousemove, false) doc.body.addEventListener('mouseup', mouseup, false) - this._map(evt.pageX, evt.pageY) + + let xy = this._getRelativeCoordinates(evt) + this._map(xy.x, xy.y) } let mousemove = (evt) => { - this._move(evt.pageX, evt.pageY) + let xy = this._getRelativeCoordinates(evt) + this._move(xy.x, xy.y) } let mouseup = (evt) => { @@ -400,23 +411,22 @@ module.exports = class ColorTriangle { } _map (x, y) { - const ox = x - const oy = y - - x -= this.offset.x + this.wheelRadius - y -= this.offset.y + this.wheelRadius + let x0 = x + let y0 = y + x -= this.wheelRadius + y -= this.wheelRadius const r = M.sqrt(x * x + y * y) // Pythagoras if (r > this.triangleRadius && r < this.wheelRadius) { // Wheel this.down = 'wheel' this._fireEvent('dragstart') - this._move(ox, oy) + this._move(x0, y0) } else if (r < this.triangleRadius) { // Inner circle this.down = 'triangle' this._fireEvent('dragstart') - this._move(ox, oy) + this._move(x0, y0) } } @@ -425,8 +435,8 @@ module.exports = class ColorTriangle { return } - x -= this.offset.x + this.wheelRadius - y -= this.offset.y + this.wheelRadius + x -= this.wheelRadius + y -= this.wheelRadius let rad = M.atan2(-y, x) if (rad < 0) {