suranadira-core
Version:
Strokes, Elements, Words and Phases of Suranadira
109 lines (100 loc) • 2.74 kB
JavaScript
/**
* Suranadira Strokes.
*
* Defines a Suranadira Font.
*
* @module
*
* @link suranadira-core/strokes.suranadira.js
* @file Draws a Suranadira Stroke on a canvas.
* @memberof suranadira-core
*
* @author Armands Strazds <strazds@gmal.com>
* @since 0.0.1
*/
/**
* suranadiraStrokes, a module for drawing the Suranadira Strokes on a canvas.
*
* @exports {Object} properties
* @exports {Function} draw
*
* @since 0.0.1
* @access public
*
*/
const suranadiraStrokes = (() => {
var _properties = {
ctx: null, // canvas context
unit: 100, // units per size 1
size: 1,
lineWidth: 50,
left: 0,
top: 0,
strokeStyle: "#000",
lineCap: "round",
scaleHorizontal: 1,
scaleVertical: 1,
shadowColor: "#fff",
shadowBlur: 0
};
/**
* Draws a stroke on a canvas
*
* @since 0.0.1
* @access private
*
* @param {String} type, the stroke @see strokes.suranadira.js
* @param {Number} x, the x-position of the Stroke
* @param {Number} y, the y-position of the Stroke
*/
const _draw = (type, x, y) => {
let ctx = _properties.ctx;
if (ctx === null) {
console.log("Please supply canvas context");
return;
}
x *= _properties.unit;
y *= _properties.unit;
let size = _properties.size * _properties.unit;
x += _properties.left;
y += _properties.top;
ctx.lineWidth = _properties.lineWidth;
ctx.strokeStyle = _properties.strokeStyle;
ctx.lineCap = _properties.lineCap;
ctx.scale(_properties.scaleHorizontal, _properties.scaleVertical);
ctx.fillStyle = _properties.fillStyle;
ctx.shadowColor = _properties.shadowColor;
ctx.shadowBlur = _properties.shadowBlur;
ctx.beginPath();
switch (type) {
case "V":
ctx.arc(x, y, size, 0 * Math.PI, 1 * Math.PI);
break;
case "A":
ctx.arc(x, y, size, 1 * Math.PI, 0 * Math.PI);
break;
case "Z":
ctx.arc(x, y, size, 0 * Math.PI, 0.5 * Math.PI);
break;
case "S":
ctx.arc(x + _properties.unit, y, size, 0.5 * Math.PI, 1 * Math.PI);
break;
case "Z+":
ctx.arc(x, y + _properties.unit, size, 1 * Math.PI, 1.5 * Math.PI);
break;
case "S+":
ctx.arc(x, y + _properties.unit, size, 1.5 * Math.PI, 0 * Math.PI);
break;
default:
// type: I
ctx.moveTo(x, y);
ctx.lineTo(x, y + size);
}
ctx.stroke();
};
return {
properties: _properties,
draw: _draw
};
})();
export default suranadiraStrokes;