@disjukr/croquis-js
Version:
drawing tool library
72 lines (71 loc) • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var stylus_1 = require("../stylus");
exports.defaultPulledStringConfig = {
stringLength: 50,
};
function getDrawingContext(stroke, config, state) {
return {
getConfig: function (target) {
if (!target || target === stroke)
return config;
return state.targetDrawingContext.getConfig(target);
},
getState: function (target) {
if (!target || target === stroke)
return state;
return state.targetDrawingContext.getState(target);
},
move: function (stylusState) {
var l = config.stringLength;
var px = state.follower.x;
var py = state.follower.y;
var dx = stylusState.x - px;
var dy = stylusState.y - py;
var d = Math.sqrt(dx * dx + dy * dy);
stylus_1.copyStylusState(state.follower, stylusState);
if (d > l) {
var t = Math.min((d - l) / l, 1);
state.follower.x = px + dx * t;
state.follower.y = py + dy * t;
}
else {
state.follower.x = px;
state.follower.y = py;
}
state.targetDrawingContext.move(state.follower);
},
up: state.targetDrawingContext.up,
};
}
function getStroke(target) {
var stroke = {
resume: function (config, prevState) {
return getDrawingContext(stroke, config, prevState);
},
down: function (config, stylusState) {
var state = {
targetDrawingContext: target.down(config.targetConfig, stylusState),
follower: stylus_1.cloneStylusState(stylusState),
};
return getDrawingContext(stroke, config, state);
},
};
return stroke;
}
exports.getStroke = getStroke;
function getGuidePathData(pointerX, pointerY, followerX, followerY, stringLength) {
var dx = pointerX - followerX;
var dy = pointerY - followerY;
var d = Math.sqrt(dx * dx + dy * dy);
var sx = followerX;
var sy = followerY;
var ex = pointerX;
var ey = pointerY;
if (d >= stringLength)
return "M" + sx + "," + sy + "L" + ex + "," + ey;
var cx = sx + (ex - sx) * 0.5;
var cy = sy + (ey - sy) * 0.5 + (stringLength - d);
return "M" + sx + "," + sy + "Q" + cx + "," + cy + " " + ex + "," + ey;
}
exports.getGuidePathData = getGuidePathData;