From 98725b3ee42a91cd1d59e1e1ec0ec2a0543e5db8 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Wed, 26 Oct 2016 14:26:48 +0800 Subject: [PATCH] fix getPastelColor function (I think) (#850) * fix getPastelColor function (I think) * fix pixelsToCoords and coordsToPixels function calls * update signature of coord/pixel functions in Util.spec.js --- frontend/src/Metamaps/JIT.js | 50 ++++++++++++++--------------- frontend/src/Metamaps/Util.js | 8 ++--- frontend/test/Metamaps.Util.spec.js | 8 ++--- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/frontend/src/Metamaps/JIT.js b/frontend/src/Metamaps/JIT.js index f4fe1540..25029e5e 100644 --- a/frontend/src/Metamaps/JIT.js +++ b/frontend/src/Metamaps/JIT.js @@ -772,17 +772,17 @@ const JIT = { // if it's a left click, or a touch, move the node if (e.touches || (e.button === 0 && !e.altKey && !e.ctrlKey && (e.buttons === 0 || e.buttons === 1 || e.buttons === undefined))) { - var width = Metamaps.Visualize.mGraph.canvas.getSize().width, - height = Metamaps.Visualize.mGraph.canvas.getSize().height, - xPix = Metamaps.Util.coordsToPixels(pos).x, - yPix = Metamaps.Util.coordsToPixels(pos).y; + var width = Visualize.mGraph.canvas.getSize().width, + height = Visualize.mGraph.canvas.getSize().height, + xPix = Util.coordsToPixels(Visualize.mGraph, pos).x, + yPix = Util.coordsToPixels(Visualize.mGraph, pos).y; if(self.dragFlag === 0){ - self.mouseDownPix = Metamaps.Util.coordsToPixels(eventInfo.getPos()); + self.mouseDownPix = Util.coordsToPixels(eventInfo.getPos()); self.dragFlag = 1; } - if(Metamaps.Util.getDistance(Metamaps.Util.coordsToPixels(pos),self.mouseDownPix) > 2 && !self.dragTolerance){ + if(Util.getDistance(Util.coordsToPixels(pos),self.mouseDownPix) > 2 && !self.dragTolerance){ self.dragTolerance = 1; } @@ -791,14 +791,14 @@ const JIT = { clearInterval(self.dragRightEdge); clearInterval(self.dragTopEdge); clearInterval(self.dragBottomEdge); - self.virtualPointer = {x:Metamaps.Util.pixelsToCoords({x:EDGE_THICKNESS,y:yPix}).x - SHIFT,y:pos.y}; - Metamaps.Visualize.mGraph.canvas.translate(SHIFT,0); + self.virtualPointer = { x: Util.pixelsToCoords(Visualize.mGraph, { x: EDGE_THICKNESS, y: yPix }).x - SHIFT, y: pos.y } + Visualize.mGraph.canvas.translate(SHIFT, 0); self.updateTopicPositions(node, self.virtualPointer); Visualize.mGraph.plot(); self.dragLeftEdge = setInterval( function(){ - self.virtualPointer = {x:Metamaps.Util.pixelsToCoords({x:EDGE_THICKNESS,y:yPix}).x - SHIFT,y:pos.y}; - Metamaps.Visualize.mGraph.canvas.translate(SHIFT,0); + self.virtualPointer = { x: Util.pixelsToCoords(Visualize.mGraph, { x: EDGE_THICKNESS, y: yPix }).x - SHIFT, y: pos.y } + Visualize.mGraph.canvas.translate(SHIFT, 0); self.updateTopicPositions(node,self.virtualPointer); Visualize.mGraph.plot(); } , PERIOD); @@ -809,14 +809,14 @@ const JIT = { clearInterval(self.dragRightEdge); clearInterval(self.dragTopEdge); clearInterval(self.dragBottomEdge); - self.virtualPointer = {x:Metamaps.Util.pixelsToCoords({x:width - EDGE_THICKNESS,y:yPix}).x + SHIFT,y:pos.y}; - Metamaps.Visualize.mGraph.canvas.translate(-SHIFT,0); + self.virtualPointer = { x: Util.pixelsToCoords(Visualize.mGraph, { x: width - EDGE_THICKNESS, y: yPix }).x + SHIFT, y: pos.y } + Visualize.mGraph.canvas.translate(-SHIFT, 0); self.updateTopicPositions(node, self.virtualPointer); Visualize.mGraph.plot(); self.dragRightEdge = setInterval( function(){ - self.virtualPointer = {x:Metamaps.Util.pixelsToCoords({x:width - EDGE_THICKNESS,y:yPix}).x + SHIFT,y:pos.y}; - Metamaps.Visualize.mGraph.canvas.translate(-SHIFT,0); + self.virtualPointer = { x: Util.pixelsToCoords(Visualize.mGraph, { x: width - EDGE_THICKNESS, y: yPix }).x + SHIFT, y: pos.y } + Visualize.mGraph.canvas.translate(-SHIFT, 0); self.updateTopicPositions(node, self.virtualPointer); Visualize.mGraph.plot(); } , PERIOD); @@ -826,14 +826,14 @@ const JIT = { clearInterval(self.dragRightEdge); clearInterval(self.dragTopEdge); clearInterval(self.dragBottomEdge); - self.virtualPointer = {x:pos.x,y:Metamaps.Util.pixelsToCoords({x:xPix,y:EDGE_THICKNESS}).y - SHIFT}; - Metamaps.Visualize.mGraph.canvas.translate(0,SHIFT); + self.virtualPointer = { x:pos.x, y: Util.pixelsToCoords(Visualize.mGraph, { x: xPix, y: EDGE_THICKNESS }).y - SHIFT } + Visualize.mGraph.canvas.translate(0, SHIFT); self.updateTopicPositions(node, self.virtualPointer); Visualize.mGraph.plot(); self.dragTopEdge = setInterval( function(){ - self.virtualPointer = {x:pos.x,y:Metamaps.Util.pixelsToCoords({x:xPix,y:EDGE_THICKNESS}).y - SHIFT}; - Metamaps.Visualize.mGraph.canvas.translate(0,SHIFT); + self.virtualPointer = { x: pos.x, y: Util.pixelsToCoords(Visualize.mGraph, { x: xPix, y: EDGE_THICKNESS }).y - SHIFT } + Visualize.mGraph.canvas.translate(0, SHIFT); self.updateTopicPositions(node, self.virtualPointer); Visualize.mGraph.plot(); } , PERIOD); @@ -843,20 +843,20 @@ const JIT = { clearInterval(self.dragRightEdge); clearInterval(self.dragTopEdge); clearInterval(self.dragBottomEdge); - self.virtualPointer = {x:pos.x,y:Metamaps.Util.pixelsToCoords({x:xPix,y:height - EDGE_THICKNESS}).y + SHIFT}; - Metamaps.Visualize.mGraph.canvas.translate(0,-SHIFT); + self.virtualPointer = { x: pos.x, y: Util.pixelsToCoords(Visualize.mGraph, { x: xPix, y: height - EDGE_THICKNESS }).y + SHIFT } + Visualize.mGraph.canvas.translate(0, -SHIFT); self.updateTopicPositions(node, self.virtualPointer); Visualize.mGraph.plot(); - self.dragBottomEdge = setInterval( function(){ - self.virtualPointer = {x:pos.x,y:Metamaps.Util.pixelsToCoords({x:xPix,y:height - EDGE_THICKNESS}).y + SHIFT}; - Metamaps.Visualize.mGraph.canvas.translate(0,-SHIFT); + self.dragBottomEdge = setInterval(function () { + self.virtualPointer = { x: pos.x, y: Util.pixelsToCoords(Visualize.mGraph, { x: xPix, y: height - EDGE_THICKNESS }).y + SHIFT } + Visualize.mGraph.canvas.translate(0, -SHIFT); self.updateTopicPositions(node, self.virtualPointer); Visualize.mGraph.plot(); - } , PERIOD); + }, PERIOD) } - if(xPix >= EDGE_THICKNESS && width - xPix >= EDGE_THICKNESS && yPix >= EDGE_THICKNESS && height - yPix >= EDGE_THICKNESS){ + if(xPix >= EDGE_THICKNESS && width - xPix >= EDGE_THICKNESS && yPix >= EDGE_THICKNESS && height - yPix >= EDGE_THICKNESS) { clearInterval(self.dragLeftEdge); clearInterval(self.dragRightEdge); clearInterval(self.dragTopEdge); diff --git a/frontend/src/Metamaps/Util.js b/frontend/src/Metamaps/Util.js index e1e43141..15467cb9 100644 --- a/frontend/src/Metamaps/Util.js +++ b/frontend/src/Metamaps/Util.js @@ -88,10 +88,10 @@ const Util = { } return coords }, - getPastelColor: function ({ rseed, gseed, bseed }) { - if (rseed === undefined) rseed = Math.random() - if (gseed === undefined) gseed = Math.random() - if (bseed === undefined) bseed = Math.random() + getPastelColor: function (opts = {}) { + const rseed = opts.rseed === undefined ? Math.random() : opts.rseed + const gseed = opts.gseed === undefined ? Math.random() : opts.gseed + const bseed = opts.bseed === undefined ? Math.random() : opts.bseed var r = (Math.round(rseed * 127) + 127).toString(16) var g = (Math.round(gseed * 127) + 127).toString(16) var b = (Math.round(bseed * 127) + 127).toString(16) diff --git a/frontend/test/Metamaps.Util.spec.js b/frontend/test/Metamaps.Util.spec.js index cc1f5839..effd5e30 100644 --- a/frontend/test/Metamaps.Util.spec.js +++ b/frontend/test/Metamaps.Util.spec.js @@ -44,15 +44,15 @@ describe('Metamaps.Util.js', function () { }) describe('coordsToPixels', function () { it('returns 0,0 for null canvas', function () { - expect(Util.coordsToPixels(null).x).to.equal(0) - expect(Util.coordsToPixels(null).y).to.equal(0) + expect(Util.coordsToPixels(null, {}).x).to.equal(0) + expect(Util.coordsToPixels(null, {}).y).to.equal(0) }) it.skip('TODO need initialized mGraph to test further') }) describe('pixelsToCoords', function () { it('returns 0,0 for null canvas', function () { - expect(Util.pixelsToCoords(null).x).to.equal(0) - expect(Util.pixelsToCoords(null).y).to.equal(0) + expect(Util.pixelsToCoords(null, {}).x).to.equal(0) + expect(Util.pixelsToCoords(null, {}).y).to.equal(0) }) it.skip('TODO need initialized mGraph to test further') })