created brush related elms; added brush resret logic; refactored naming convention around brush lift state
This commit is contained in:
@@ -93,7 +93,7 @@ class ForwardElement extends BaseInputScriptElement {
|
|||||||
Painter.x1 = x2
|
Painter.x1 = x2
|
||||||
Painter.y1 = y2
|
Painter.y1 = y2
|
||||||
|
|
||||||
if (Painter.isPenLifted) {
|
if (Painter.isBrushLifted) {
|
||||||
Painter.canvasContext.moveTo(x2, y2);
|
Painter.canvasContext.moveTo(x2, y2);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -117,7 +117,7 @@ class BackElement extends BaseInputScriptElement {
|
|||||||
Painter.x1 = x2
|
Painter.x1 = x2
|
||||||
Painter.y1 = y2
|
Painter.y1 = y2
|
||||||
|
|
||||||
if (Painter.isPenLifted) {
|
if (Painter.isBrushLifted) {
|
||||||
Painter.canvasContext.moveTo(x2, y2);
|
Painter.canvasContext.moveTo(x2, y2);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -143,13 +143,13 @@ class CircleElement extends BaseInputScriptElement {
|
|||||||
class PenUpElement extends BaseScriptElement {
|
class PenUpElement extends BaseScriptElement {
|
||||||
get prefix() { return 'Pen Up'; }
|
get prefix() { return 'Pen Up'; }
|
||||||
constructor() { super(); }
|
constructor() { super(); }
|
||||||
action(Painter) { Painter.isPenLifted = true; }
|
action(Painter) { Painter.isBrushLifted = true; }
|
||||||
}
|
}
|
||||||
|
|
||||||
class PenDownElement extends BaseScriptElement {
|
class PenDownElement extends BaseScriptElement {
|
||||||
get prefix() { return 'Pen Down'; }
|
get prefix() { return 'Pen Down'; }
|
||||||
constructor() { super(); }
|
constructor() { super(); }
|
||||||
action(Painter) { Painter.isPenLifted = false; }
|
action(Painter) { Painter.isBrushLifted = false; }
|
||||||
}
|
}
|
||||||
|
|
||||||
class BackToCenterElement extends BaseScriptElement {
|
class BackToCenterElement extends BaseScriptElement {
|
||||||
@@ -162,7 +162,42 @@ class BackToCenterElement extends BaseScriptElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HideTurtleElement extends BaseScriptElement {
|
class BrushSizeElement extends BaseInputScriptElement {
|
||||||
get prefix() { return 'Hide Turtle'; }
|
get prefix() { return 'Brush Size'; }
|
||||||
|
get defaultValue() { return 24; }
|
||||||
|
get postfix() { return 'px'; }
|
||||||
constructor() { super(); }
|
constructor() { super(); }
|
||||||
|
connectedCallback() {
|
||||||
|
this.innerHTML = `
|
||||||
|
<li>
|
||||||
|
<label>${this.prefix}</label>
|
||||||
|
<input type="text" class="script-input" value="${this.defaultValue}"/>
|
||||||
|
<label>${this.postfix}</label>
|
||||||
|
</li>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
action(Painter) {
|
||||||
|
Painter.brushSize = this.value;
|
||||||
|
Painter.canvasContext.lineWidth = this.value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class BrushColorElement extends BaseInputScriptElement {
|
||||||
|
get prefix() { return 'Brush Color'; }
|
||||||
|
get defaultValue() { return '#000000'; }
|
||||||
|
constructor() { super(); }
|
||||||
|
connectedCallback() {
|
||||||
|
this.innerHTML = `
|
||||||
|
<li>
|
||||||
|
<label>${this.prefix}</label>
|
||||||
|
<input type="color" class="script-input" value="${this.defaultValue}"/>
|
||||||
|
<label>${this.postfix}</label>
|
||||||
|
</li>
|
||||||
|
`;
|
||||||
|
}
|
||||||
|
action(Painter) {
|
||||||
|
Painter.brushColor = this.value;
|
||||||
|
Painter.canvasContext.fillStyle = this.value;
|
||||||
|
Painter.canvasContext.strokeStyle = this.value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,29 @@
|
|||||||
const ScriptElms = {
|
const ScriptElms = {
|
||||||
"repeat-element": RepeatElement,
|
"brush-size-element": BrushSizeElement,
|
||||||
"left-element": LeftElement,
|
"brush-color-element": BrushColorElement,
|
||||||
"right-element": RightElement,
|
"back-to-center-element": BackToCenterElement,
|
||||||
"forward-element": ForwardElement,
|
|
||||||
"back-element": BackElement,
|
|
||||||
"circle-element": CircleElement,
|
"circle-element": CircleElement,
|
||||||
"pen-up-element": PenUpElement,
|
"pen-up-element": PenUpElement,
|
||||||
"pen-down-element": PenDownElement,
|
"pen-down-element": PenDownElement,
|
||||||
"back-to-center-element": BackToCenterElement,
|
"repeat-element": RepeatElement,
|
||||||
"hider-turtle-element": HideTurtleElement
|
"forward-element": ForwardElement,
|
||||||
|
"back-element": BackElement,
|
||||||
|
"left-element": LeftElement,
|
||||||
|
"right-element": RightElement
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const BRUSH_SIZE = 2;
|
||||||
|
const BRUSH_COLOR = "#000000";
|
||||||
|
|
||||||
let Painter = {
|
let Painter = {
|
||||||
canvas: null,
|
canvas: null,
|
||||||
canvasContext: null,
|
canvasContext: null,
|
||||||
|
brushSize: BRUSH_SIZE,
|
||||||
|
brushColor: BRUSH_COLOR,
|
||||||
rotation: 0,
|
rotation: 0,
|
||||||
x1: 0,
|
x1: 0,
|
||||||
y1: 0,
|
y1: 0,
|
||||||
isPenLifted: false
|
isBrushLifted: false
|
||||||
}
|
}
|
||||||
|
|
||||||
let draggedElm = null;
|
let draggedElm = null;
|
||||||
|
|||||||
@@ -5,16 +5,36 @@ const setupScriptRunner = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const runScript = () => {
|
const runScript = () => {
|
||||||
setToCenter();
|
resetCanvasState();
|
||||||
resetRotation();
|
|
||||||
clearCanvas();
|
|
||||||
|
|
||||||
for (const elm of script.children) {
|
for (const elm of script.children) {
|
||||||
elm.action(Painter);
|
elm.action(Painter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const setToCenter = () => {
|
const resetCanvasState = () => {
|
||||||
|
resetRotation();
|
||||||
|
resetCanvasBrush();
|
||||||
|
clearCanvas();
|
||||||
|
}
|
||||||
|
|
||||||
|
const resetCanvasBrush = () => {
|
||||||
|
setDefaultBrushSize();
|
||||||
|
setDefaultBrushColor();
|
||||||
|
setBrushToCenter();
|
||||||
|
}
|
||||||
|
|
||||||
|
const setDefaultBrushSize = () => {
|
||||||
|
Painter.brushSize = BRUSH_SIZE;
|
||||||
|
Painter.canvasContext.lineWidth = BRUSH_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
const setDefaultBrushColor = () => {
|
||||||
|
Painter.brushColor = BRUSH_COLOR;
|
||||||
|
Painter.canvasContext.fillStyle = BRUSH_COLOR;
|
||||||
|
Painter.canvasContext.strokeStyle = BRUSH_COLOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
const setBrushToCenter = () => {
|
||||||
Painter.x1 = Painter.canvas.width / 2
|
Painter.x1 = Painter.canvas.width / 2
|
||||||
Painter.y1 = Painter.canvas.height / 2
|
Painter.y1 = Painter.canvas.height / 2
|
||||||
Painter.canvasContext.moveTo( Painter.x1, Painter.y1 );
|
Painter.canvasContext.moveTo( Painter.x1, Painter.y1 );
|
||||||
|
|||||||
Reference in New Issue
Block a user