@disjukr/croquis-js
Version:
drawing tool library
77 lines (76 loc) • 2.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var stylus_1 = require("../stylus");
var stylus_2 = require("../stylus");
exports.defaultSnakeConfig = {
tailCount: 3,
weight: 0.5,
catchUp: true,
};
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 head = state.stylusStates[0];
stylus_2.copyStylusState(head, stylusState);
},
up: function (stylusState) {
var head = state.stylusStates[0];
var tail = state.stylusStates[state.stylusStates.length - 1];
stylus_2.copyStylusState(head, stylusState);
if (config.catchUp) {
var dx = void 0, dy = void 0;
do {
state.update();
dx = (tail.x - head.x) | 0;
dy = (tail.y - head.y) | 0;
} while (dx || dy);
}
return state.targetDrawingContext.up(tail);
},
};
}
function getStroke(target) {
var stroke = {
resume: function (config, prevState) {
var diff = config.tailCount + 1 - prevState.stylusStates.length;
if (diff > 0) {
var tail = prevState.stylusStates[prevState.stylusStates.length - 1];
prevState.stylusStates = prevState.stylusStates.concat(stylus_1.createStylusStates(diff, tail));
}
else if (diff < 0) {
prevState.stylusStates.length -= diff;
}
return getDrawingContext(stroke, config, prevState);
},
down: function (config, stylusState) {
var stylusStates = stylus_1.createStylusStates(config.tailCount + 1, stylusState);
var tail = stylusStates[stylusStates.length - 1];
var state = {
targetDrawingContext: target.down(config.targetConfig, stylusState),
stylusStates: stylusStates,
update: function () {
var follow = 1 - Math.min(0.95, Math.max(0, config.weight));
for (var i = 1; i < stylusStates.length; ++i) {
var curr = stylusStates[i];
var prev = stylusStates[i - 1];
stylus_2.interpolateStylusState(curr, prev, follow);
}
state.targetDrawingContext.move(tail);
},
};
return getDrawingContext(stroke, config, state);
},
};
return stroke;
}
exports.getStroke = getStroke;