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
This commit is contained in:
Devin Howard 2016-10-26 14:26:48 +08:00 committed by GitHub
parent 8a95262f2c
commit 98725b3ee4
3 changed files with 33 additions and 33 deletions

View file

@ -772,17 +772,17 @@ const JIT = {
// if it's a left click, or a touch, move the node // 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))) { 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, var width = Visualize.mGraph.canvas.getSize().width,
height = Metamaps.Visualize.mGraph.canvas.getSize().height, height = Visualize.mGraph.canvas.getSize().height,
xPix = Metamaps.Util.coordsToPixels(pos).x, xPix = Util.coordsToPixels(Visualize.mGraph, pos).x,
yPix = Metamaps.Util.coordsToPixels(pos).y; yPix = Util.coordsToPixels(Visualize.mGraph, pos).y;
if(self.dragFlag === 0){ if(self.dragFlag === 0){
self.mouseDownPix = Metamaps.Util.coordsToPixels(eventInfo.getPos()); self.mouseDownPix = Util.coordsToPixels(eventInfo.getPos());
self.dragFlag = 1; 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; self.dragTolerance = 1;
} }
@ -791,14 +791,14 @@ const JIT = {
clearInterval(self.dragRightEdge); clearInterval(self.dragRightEdge);
clearInterval(self.dragTopEdge); clearInterval(self.dragTopEdge);
clearInterval(self.dragBottomEdge); clearInterval(self.dragBottomEdge);
self.virtualPointer = {x:Metamaps.Util.pixelsToCoords({x:EDGE_THICKNESS,y:yPix}).x - SHIFT,y:pos.y}; self.virtualPointer = { x: Util.pixelsToCoords(Visualize.mGraph, { x: EDGE_THICKNESS, y: yPix }).x - SHIFT, y: pos.y }
Metamaps.Visualize.mGraph.canvas.translate(SHIFT,0); Visualize.mGraph.canvas.translate(SHIFT, 0);
self.updateTopicPositions(node, self.virtualPointer); self.updateTopicPositions(node, self.virtualPointer);
Visualize.mGraph.plot(); Visualize.mGraph.plot();
self.dragLeftEdge = setInterval( function(){ self.dragLeftEdge = setInterval( function(){
self.virtualPointer = {x:Metamaps.Util.pixelsToCoords({x:EDGE_THICKNESS,y:yPix}).x - SHIFT,y:pos.y}; self.virtualPointer = { x: Util.pixelsToCoords(Visualize.mGraph, { x: EDGE_THICKNESS, y: yPix }).x - SHIFT, y: pos.y }
Metamaps.Visualize.mGraph.canvas.translate(SHIFT,0); Visualize.mGraph.canvas.translate(SHIFT, 0);
self.updateTopicPositions(node,self.virtualPointer); self.updateTopicPositions(node,self.virtualPointer);
Visualize.mGraph.plot(); Visualize.mGraph.plot();
} , PERIOD); } , PERIOD);
@ -809,14 +809,14 @@ const JIT = {
clearInterval(self.dragRightEdge); clearInterval(self.dragRightEdge);
clearInterval(self.dragTopEdge); clearInterval(self.dragTopEdge);
clearInterval(self.dragBottomEdge); clearInterval(self.dragBottomEdge);
self.virtualPointer = {x:Metamaps.Util.pixelsToCoords({x:width - EDGE_THICKNESS,y:yPix}).x + SHIFT,y:pos.y}; self.virtualPointer = { x: Util.pixelsToCoords(Visualize.mGraph, { x: width - EDGE_THICKNESS, y: yPix }).x + SHIFT, y: pos.y }
Metamaps.Visualize.mGraph.canvas.translate(-SHIFT,0); Visualize.mGraph.canvas.translate(-SHIFT, 0);
self.updateTopicPositions(node, self.virtualPointer); self.updateTopicPositions(node, self.virtualPointer);
Visualize.mGraph.plot(); Visualize.mGraph.plot();
self.dragRightEdge = setInterval( function(){ self.dragRightEdge = setInterval( function(){
self.virtualPointer = {x:Metamaps.Util.pixelsToCoords({x:width - EDGE_THICKNESS,y:yPix}).x + SHIFT,y:pos.y}; self.virtualPointer = { x: Util.pixelsToCoords(Visualize.mGraph, { x: width - EDGE_THICKNESS, y: yPix }).x + SHIFT, y: pos.y }
Metamaps.Visualize.mGraph.canvas.translate(-SHIFT,0); Visualize.mGraph.canvas.translate(-SHIFT, 0);
self.updateTopicPositions(node, self.virtualPointer); self.updateTopicPositions(node, self.virtualPointer);
Visualize.mGraph.plot(); Visualize.mGraph.plot();
} , PERIOD); } , PERIOD);
@ -826,14 +826,14 @@ const JIT = {
clearInterval(self.dragRightEdge); clearInterval(self.dragRightEdge);
clearInterval(self.dragTopEdge); clearInterval(self.dragTopEdge);
clearInterval(self.dragBottomEdge); clearInterval(self.dragBottomEdge);
self.virtualPointer = {x:pos.x,y:Metamaps.Util.pixelsToCoords({x:xPix,y:EDGE_THICKNESS}).y - SHIFT}; self.virtualPointer = { x:pos.x, y: Util.pixelsToCoords(Visualize.mGraph, { x: xPix, y: EDGE_THICKNESS }).y - SHIFT }
Metamaps.Visualize.mGraph.canvas.translate(0,SHIFT); Visualize.mGraph.canvas.translate(0, SHIFT);
self.updateTopicPositions(node, self.virtualPointer); self.updateTopicPositions(node, self.virtualPointer);
Visualize.mGraph.plot(); Visualize.mGraph.plot();
self.dragTopEdge = setInterval( function(){ self.dragTopEdge = setInterval( function(){
self.virtualPointer = {x:pos.x,y:Metamaps.Util.pixelsToCoords({x:xPix,y:EDGE_THICKNESS}).y - SHIFT}; self.virtualPointer = { x: pos.x, y: Util.pixelsToCoords(Visualize.mGraph, { x: xPix, y: EDGE_THICKNESS }).y - SHIFT }
Metamaps.Visualize.mGraph.canvas.translate(0,SHIFT); Visualize.mGraph.canvas.translate(0, SHIFT);
self.updateTopicPositions(node, self.virtualPointer); self.updateTopicPositions(node, self.virtualPointer);
Visualize.mGraph.plot(); Visualize.mGraph.plot();
} , PERIOD); } , PERIOD);
@ -843,20 +843,20 @@ const JIT = {
clearInterval(self.dragRightEdge); clearInterval(self.dragRightEdge);
clearInterval(self.dragTopEdge); clearInterval(self.dragTopEdge);
clearInterval(self.dragBottomEdge); clearInterval(self.dragBottomEdge);
self.virtualPointer = {x:pos.x,y:Metamaps.Util.pixelsToCoords({x:xPix,y:height - EDGE_THICKNESS}).y + SHIFT}; self.virtualPointer = { x: pos.x, y: Util.pixelsToCoords(Visualize.mGraph, { x: xPix, y: height - EDGE_THICKNESS }).y + SHIFT }
Metamaps.Visualize.mGraph.canvas.translate(0,-SHIFT); Visualize.mGraph.canvas.translate(0, -SHIFT);
self.updateTopicPositions(node, self.virtualPointer); self.updateTopicPositions(node, self.virtualPointer);
Visualize.mGraph.plot(); Visualize.mGraph.plot();
self.dragBottomEdge = setInterval( function(){ self.dragBottomEdge = setInterval(function () {
self.virtualPointer = {x:pos.x,y:Metamaps.Util.pixelsToCoords({x:xPix,y:height - EDGE_THICKNESS}).y + SHIFT}; self.virtualPointer = { x: pos.x, y: Util.pixelsToCoords(Visualize.mGraph, { x: xPix, y: height - EDGE_THICKNESS }).y + SHIFT }
Metamaps.Visualize.mGraph.canvas.translate(0,-SHIFT); Visualize.mGraph.canvas.translate(0, -SHIFT);
self.updateTopicPositions(node, self.virtualPointer); self.updateTopicPositions(node, self.virtualPointer);
Visualize.mGraph.plot(); 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.dragLeftEdge);
clearInterval(self.dragRightEdge); clearInterval(self.dragRightEdge);
clearInterval(self.dragTopEdge); clearInterval(self.dragTopEdge);

View file

@ -88,10 +88,10 @@ const Util = {
} }
return coords return coords
}, },
getPastelColor: function ({ rseed, gseed, bseed }) { getPastelColor: function (opts = {}) {
if (rseed === undefined) rseed = Math.random() const rseed = opts.rseed === undefined ? Math.random() : opts.rseed
if (gseed === undefined) gseed = Math.random() const gseed = opts.gseed === undefined ? Math.random() : opts.gseed
if (bseed === undefined) bseed = Math.random() const bseed = opts.bseed === undefined ? Math.random() : opts.bseed
var r = (Math.round(rseed * 127) + 127).toString(16) var r = (Math.round(rseed * 127) + 127).toString(16)
var g = (Math.round(gseed * 127) + 127).toString(16) var g = (Math.round(gseed * 127) + 127).toString(16)
var b = (Math.round(bseed * 127) + 127).toString(16) var b = (Math.round(bseed * 127) + 127).toString(16)

View file

@ -44,15 +44,15 @@ describe('Metamaps.Util.js', function () {
}) })
describe('coordsToPixels', function () { describe('coordsToPixels', function () {
it('returns 0,0 for null canvas', function () { it('returns 0,0 for null canvas', function () {
expect(Util.coordsToPixels(null).x).to.equal(0) expect(Util.coordsToPixels(null, {}).x).to.equal(0)
expect(Util.coordsToPixels(null).y).to.equal(0) expect(Util.coordsToPixels(null, {}).y).to.equal(0)
}) })
it.skip('TODO need initialized mGraph to test further') it.skip('TODO need initialized mGraph to test further')
}) })
describe('pixelsToCoords', function () { describe('pixelsToCoords', function () {
it('returns 0,0 for null canvas', function () { it('returns 0,0 for null canvas', function () {
expect(Util.pixelsToCoords(null).x).to.equal(0) expect(Util.pixelsToCoords(null, {}).x).to.equal(0)
expect(Util.pixelsToCoords(null).y).to.equal(0) expect(Util.pixelsToCoords(null, {}).y).to.equal(0)
}) })
it.skip('TODO need initialized mGraph to test further') it.skip('TODO need initialized mGraph to test further')
}) })