scratch-paint/src/helper/math.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-10-12 18:35:30 -04:00
import paper from '@scratch/paper';
2017-09-11 14:23:30 -04:00
const checkPointsClose = function (startPos, eventPoint, threshold) {
const xOff = Math.abs(startPos.x - eventPoint.x);
const yOff = Math.abs(startPos.y - eventPoint.y);
if (xOff < threshold && yOff < threshold) {
return true;
}
return false;
};
const getRandomInt = function (min, max) {
return Math.floor(Math.random() * (max - min)) + min;
};
const getRandomBoolean = function () {
return getRandomInt(0, 2) === 1;
};
// Thanks Mikko Mononen! https://github.com/memononen/stylii
const snapDeltaToAngle = function (delta, snapAngle) {
let angle = Math.atan2(delta.y, delta.x);
angle = Math.round(angle / snapAngle) * snapAngle;
const dirx = Math.cos(angle);
const diry = Math.sin(angle);
const d = (dirx * delta.x) + (diry * delta.y);
return new paper.Point(dirx * d, diry * d);
};
2017-11-03 17:55:02 -04:00
const sortItemsByZIndex = function (a, b) {
if (a === null || b === null) {
// Incomparable
return null;
}
let tempA = a;
let tempB = b;
while (!(tempA instanceof paper.Layer)) {
while (!(tempB instanceof paper.Layer)) {
if (tempB === tempA) {
return 0;
} else if (tempB.parent === tempA.parent) {
return parseFloat(tempA.index) - parseFloat(tempB.index);
}
tempB = tempB.parent;
}
tempA = tempA.parent;
}
// No shared hierarchy
return null;
};
2017-09-11 14:23:30 -04:00
export {
checkPointsClose,
getRandomInt,
getRandomBoolean,
2017-11-03 17:55:02 -04:00
snapDeltaToAngle,
sortItemsByZIndex
2017-09-11 14:23:30 -04:00
};