kopia lustrzana https://github.com/backface/turtlestitch
add more stitch types
rodzic
d1849e33d6
commit
ec5ec2205f
|
@ -40,6 +40,7 @@ SpriteMorph.prototype.init = function(globals) {
|
|||
this.cache = new Cache;
|
||||
this.color = new Color(0,0,0,1);
|
||||
this.stitchtype = 0;
|
||||
this.isRunning = false;
|
||||
this.stitchoptions = {};
|
||||
};
|
||||
|
||||
|
@ -308,14 +309,73 @@ SpriteMorph.prototype.clearStitchSettings = function () {
|
|||
|
||||
SpriteMorph.prototype.runningStitch = function (length, autoadjust) {
|
||||
if (length > 0) {
|
||||
this.stitchtype = "running";
|
||||
this.stitchoptions = { length: length, autoadjust: autoadjust }
|
||||
this.isRunning = true;
|
||||
this.stitchoptions = {
|
||||
length: length,
|
||||
autoadjust: true,
|
||||
type:"single"
|
||||
}
|
||||
} else {
|
||||
this.stitchtype = 0;
|
||||
this.stitchoptions = {};
|
||||
}
|
||||
}
|
||||
|
||||
SpriteMorph.prototype.beanStitch = function (length, autoadjust=true) {
|
||||
if (length > 0) {
|
||||
this.stitchtype = "bean";
|
||||
this.isRunning = true;
|
||||
this.stitchoptions = {
|
||||
length: length,
|
||||
autoadjust: autoadjust
|
||||
}
|
||||
} else {
|
||||
this.stitchtype = 0;
|
||||
this.stitchoptions = {};
|
||||
}
|
||||
}
|
||||
|
||||
SpriteMorph.prototype.crossStitch = function (length, width=10, center=true, autoadjust=true) {
|
||||
if (length > 0 && width > 0) {
|
||||
this.stitchtype = "cross";
|
||||
this.isRunning = true;
|
||||
this.stitchoptions = {
|
||||
length: length,
|
||||
autoadjust: autoadjust,
|
||||
width: width,
|
||||
center: center,
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
SpriteMorph.prototype.zigzagStitch = function (density, width=10, center=true, autoadjust=true) {
|
||||
if (density > 0 && width > 0) {
|
||||
this.stitchtype = "zigzag";
|
||||
this.isRunning = true;
|
||||
this.stitchoptions = {
|
||||
center: center,
|
||||
autoadjust: autoadjust,
|
||||
width: width,
|
||||
length: density,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SpriteMorph.prototype.satinStitch = function (width=10, center=true, autoadjust=true) {
|
||||
if (width > 0) {
|
||||
this.stitchtype = "zigzag";
|
||||
this.isRunning = true;
|
||||
this.stitchoptions = {
|
||||
autoadjust: true,
|
||||
width: width,
|
||||
length: 2,
|
||||
center: center,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SpriteMorph.prototype.jumpStitch = function (on = true) {
|
||||
this.isDown = !on;
|
||||
}
|
||||
|
@ -347,7 +407,7 @@ SpriteMorph.prototype.forward = function (steps) {
|
|||
}
|
||||
|
||||
if (dist != 0) {
|
||||
if ( this.stitchtype == "running" && dist > this.stitchoptions.length && this.isDown) {
|
||||
if ( this.isRunning && dist > this.stitchoptions.length && this.isDown) {
|
||||
if (this.stitchoptions.autoadjust) {
|
||||
var real_length = dist / Math.round(dist / this.stitchoptions.length);
|
||||
this.forwardBy(steps, real_length);
|
||||
|
@ -360,7 +420,120 @@ SpriteMorph.prototype.forward = function (steps) {
|
|||
}
|
||||
};
|
||||
|
||||
|
||||
SpriteMorph.prototype.forwardByNr = function (totalsteps, nr_steps) {
|
||||
stepsize = totalsteps / nr_steps;
|
||||
for(i=0;i<nr_steps;i++) {
|
||||
if (this.stitchtype == "zigzag" && i == 0)
|
||||
this.zigzagForwardStart(stepsize, this.stitchoptions.width)
|
||||
else if (this.stitchtype == "zigzag" && i == nr_steps - 1)
|
||||
this.zigzagForwardEnd(stepsize, this.stitchoptions.width)
|
||||
else
|
||||
this.moveforward(stepsize);
|
||||
}
|
||||
};
|
||||
|
||||
SpriteMorph.prototype.forwardBy = function (totalsteps, stepsize) {
|
||||
nr_steps = Math.floor(totalsteps / stepsize);
|
||||
rest = totalsteps - (nr_steps * stepsize);
|
||||
for(i=0;i<nr_steps;i++) {
|
||||
if (this.stitchtype == "cross" && i == 0 && this.stitchoptions.center)
|
||||
this.crossStitchForwardStart(stepsize, this.stitchoptions.width)
|
||||
|
||||
if (this.stitchtype == "zigzag" && i == 0 && this.stitchoptions.center)
|
||||
this.zigzagForwardStart(stepsize, this.stitchoptions.width)
|
||||
else
|
||||
this.moveforward(stepsize);
|
||||
|
||||
if (this.stitchtype == "zigzag" && i == nr_steps - 1 && this.stitchoptions.center)
|
||||
this.zigzagForwardEnd(stepsize, this.stitchoptions.width)
|
||||
|
||||
if (this.stitchtype == "cross" && i == nr_steps - 1 && this.stitchoptions.center)
|
||||
this.crossStitchForwardStop(stepsize, this.stitchoptions.width)
|
||||
}
|
||||
if (rest > 0) {
|
||||
this.moveforward(rest);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
SpriteMorph.prototype.beanStitchForward = function (steps) {
|
||||
this.doMoveForward(steps);
|
||||
this.doMoveForward(-steps);
|
||||
this.doMoveForward(steps);
|
||||
}
|
||||
|
||||
SpriteMorph.prototype.crossStitchForward = function (steps, width=10) {
|
||||
var c = Math.sqrt(steps*steps + width * width);
|
||||
var alpha = degrees(Math.asin(width/c));
|
||||
|
||||
this.turn(alpha);
|
||||
this.doMoveForward(c);
|
||||
this.turn(180 - alpha);
|
||||
this.doMoveForward(steps);
|
||||
this.turn(180 - alpha);
|
||||
this.doMoveForward(c);
|
||||
this.turn(alpha);
|
||||
}
|
||||
|
||||
SpriteMorph.prototype.zigzagForward = function (steps, width=10) {
|
||||
var c = Math.sqrt(steps/2*steps/2 + width * width);
|
||||
var alpha = degrees(Math.asin(width/c));
|
||||
|
||||
this.turn(alpha);
|
||||
this.doMoveForward(c);
|
||||
this.turnLeft(2 *alpha);
|
||||
this.doMoveForward(c);
|
||||
this.turn(alpha);
|
||||
}
|
||||
|
||||
SpriteMorph.prototype.crossStitchForwardStart = function (steps, width=10) {
|
||||
this.turn(-90);
|
||||
this.doMoveForward(width/2);
|
||||
this.turn(90);
|
||||
}
|
||||
|
||||
SpriteMorph.prototype.crossStitchForwardStop = function (steps, width=10) {
|
||||
this.turn(90);
|
||||
this.doMoveForward(width/2);
|
||||
this.turn(-90);
|
||||
}
|
||||
|
||||
SpriteMorph.prototype.zigzagForwardStart = function (steps, width=10) {
|
||||
var c = Math.sqrt(steps/2*steps/2 + width * width);
|
||||
var alpha = degrees(Math.asin(width/c));
|
||||
|
||||
this.turnLeft(alpha);
|
||||
this.doMoveForward(c/2);
|
||||
this.turn(alpha);
|
||||
}
|
||||
|
||||
SpriteMorph.prototype.zigzagForwardEnd = function (steps, width=10) {
|
||||
var c = Math.sqrt(steps/2*steps/2 + width * width);
|
||||
var alpha = degrees(Math.asin(width/c));
|
||||
|
||||
this.turn(alpha);
|
||||
this.doMoveForward(c);
|
||||
this.turnLeft(2 *alpha);
|
||||
this.doMoveForward(c/2);
|
||||
this.turn(alpha);
|
||||
}
|
||||
|
||||
|
||||
SpriteMorph.prototype.moveforward = function (steps) {
|
||||
if ( this.stitchtype == "bean") {
|
||||
this.beanStitchForward(steps);
|
||||
} else if ( this.stitchtype == "cross") {
|
||||
this.crossStitchForward(steps, this.stitchoptions.width)
|
||||
} else if ( this.stitchtype == "zigzag") {
|
||||
this.zigzagForward(steps, this.stitchoptions.width)
|
||||
} else {
|
||||
this.doMoveForward(steps)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SpriteMorph.prototype.doMoveForward = function (steps) {
|
||||
var dest,
|
||||
dist = steps * this.parent.scale || 0;
|
||||
stage = this.parentThatIsA(StageMorph);
|
||||
|
@ -397,29 +570,8 @@ SpriteMorph.prototype.moveforward = function (steps) {
|
|||
}
|
||||
stage.moveTurtle(this.xPosition(), this.yPosition());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
SpriteMorph.prototype.forwardByNr = function (totalsteps, nr_steps) {
|
||||
stepsize = totalsteps / nr_steps;
|
||||
for(i=0;i<nr_steps;i++) {
|
||||
this.moveforward(stepsize);
|
||||
}
|
||||
};
|
||||
|
||||
SpriteMorph.prototype.forwardBy = function (totalsteps, stepsize) {
|
||||
nr_steps = Math.floor(totalsteps / stepsize);
|
||||
rest = totalsteps - (nr_steps * stepsize);
|
||||
for(i=0;i<nr_steps;i++) {
|
||||
this.moveforward(stepsize);
|
||||
}
|
||||
if (rest > 0) {
|
||||
this.moveforward(rest);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
SpriteMorph.prototype.origGotoXY = SpriteMorph.prototype.gotoXY;
|
||||
SpriteMorph.prototype.gotoXY = function (x, y, justMe, noShadow) {
|
||||
var stage = this.parentThatIsA(StageMorph);
|
||||
|
@ -439,12 +591,12 @@ SpriteMorph.prototype.gotoXY = function (x, y, justMe, noShadow) {
|
|||
var dist = Math.sqrt(a*a + b*b);
|
||||
if (a == 0 && b == 0) dist = 0;
|
||||
|
||||
if ( dist <= 1) {
|
||||
if ( Math.round(dist,5) <= 0.001) {
|
||||
// jump in place - don't add / ignore
|
||||
//console.log("jump in place - don't add / ignore", this.isDown,this.xPosition(), this.yPosition(), dist);
|
||||
} else {
|
||||
|
||||
if ( this.stitchtype == "running" && dist > this.stitchoptions.length && this.isDown) {
|
||||
if ( this.isRunning && dist > this.stitchoptions.length && this.isDown) {
|
||||
if (this.stitchoptions.autoadjust) {
|
||||
var real_length = dist / Math.round(dist / this.stitchoptions.length);
|
||||
stepsize = real_length;
|
||||
|
@ -454,21 +606,37 @@ SpriteMorph.prototype.gotoXY = function (x, y, justMe, noShadow) {
|
|||
var steps = Math.floor(dist / stepsize);
|
||||
var rest = dist - (steps * stepsize);
|
||||
|
||||
rest = Math.round(rest,8);
|
||||
stepsize = Math.round(stepsize,8);
|
||||
|
||||
var deltaX = (x - this.xPosition()) * this.parent.scale;
|
||||
var deltaY = (y - this.yPosition()) * this.parent.scale;
|
||||
var angle = Math.abs(deltaX) < 0.001 ? (deltaY < 0 ? 90 : 270)
|
||||
: Math.round(
|
||||
(deltaX >= 0 ? 0 : 180)
|
||||
- (Math.atan(deltaY / deltaX) * 57.2957795131)
|
||||
var angle = Math.abs(deltaX) < 0.0001 ? (deltaY < 0 ? 90 : 270)
|
||||
: Math.round( (deltaX >= 0 ? 0 : 180) - (Math.atan(deltaY / deltaX) * 57.2957795131),8
|
||||
);
|
||||
this.setHeading(angle + 90);
|
||||
|
||||
for(i=0; i < steps; i++) {
|
||||
if (this.stitchtype == "cross" && i == 0 && this.stitchoptions.center)
|
||||
this.crossStitchForwardStart(stepsize, this.stitchoptions.width)
|
||||
|
||||
if (this.stitchtype == "zigzag" && i == 0 && this.stitchoptions.center)
|
||||
this.zigzagForwardStart(stepsize, this.stitchoptions.width)
|
||||
else
|
||||
this.moveforward(stepsize);
|
||||
|
||||
if (this.stitchtype == "zigzag" && i == steps - 1 && this.stitchoptions.center)
|
||||
this.zigzagForwardEnd(stepsize, this.stitchoptions.width)
|
||||
|
||||
if (this.stitchtype == "cross" && i == steps - 1 && this.stitchoptions.center)
|
||||
this.crossStitchForwardStop(stepsize, this.stitchoptions.width)
|
||||
}
|
||||
if (rest > 0) {
|
||||
|
||||
console.log(x, y, this.xPosition(), this.yPosition())
|
||||
if (rest > 0 || x != this.xPosition() || y != this.yPosition()) {
|
||||
this.gotoXY(x,y);
|
||||
this.origGotoXY(x, y, justMe);
|
||||
console.log(x, y, this.xPosition(), this.yPosition())
|
||||
}
|
||||
|
||||
} else {
|
||||
|
@ -1541,12 +1709,13 @@ StageMorph.prototype.userMenu = function () {
|
|||
|
||||
SpriteMorph.prototype.resetAll = function () {
|
||||
var myself = this;
|
||||
myself.stitchtype = 0;
|
||||
myself.stitchoptions = {};
|
||||
myself.isRunning = false;
|
||||
myself.setColor(new Color(0, 0, 0, 1.0));
|
||||
myself.gotoXY(0,0);
|
||||
myself.setHeading(90);
|
||||
myself.clear();
|
||||
myself.setColor(new Color(0, 0, 0, 1.0));
|
||||
myself.stitchtype = 0;
|
||||
myself.stitchoptions = {};
|
||||
myself.isDown = true;
|
||||
}
|
||||
|
||||
|
@ -1555,6 +1724,7 @@ SpriteMorph.prototype.resetStitchSettings = function () {
|
|||
var myself = this;
|
||||
myself.stitchoptions = {}
|
||||
myself.stitchtype = 0;
|
||||
myself.isRunning = false;
|
||||
}
|
||||
|
||||
// Block specs
|
||||
|
@ -1779,6 +1949,42 @@ SpriteMorph.prototype.initBlocks = function () {
|
|||
defaults: [12]
|
||||
};
|
||||
|
||||
this.blocks.beanStitch =
|
||||
{
|
||||
only: SpriteMorph,
|
||||
type: 'command',
|
||||
spec: 'bean stitch by %n',
|
||||
category: 'embroidery',
|
||||
defaults: [12]
|
||||
};
|
||||
|
||||
this.blocks.crossStitch =
|
||||
{
|
||||
only: SpriteMorph,
|
||||
type: 'command',
|
||||
spec: 'cross stitch by in %n width %n center %b',
|
||||
category: 'embroidery',
|
||||
defaults: [12 , 12, true]
|
||||
};
|
||||
|
||||
this.blocks.zigzagStitch =
|
||||
{
|
||||
only: SpriteMorph,
|
||||
type: 'command',
|
||||
spec: 'zigzag with density %n width %n center %b',
|
||||
category: 'embroidery',
|
||||
defaults: [20, 20, true]
|
||||
};
|
||||
|
||||
this.blocks.satinStitch =
|
||||
{
|
||||
only: SpriteMorph,
|
||||
type: 'command',
|
||||
spec: 'satin stitch with width %n center %b',
|
||||
category: 'embroidery',
|
||||
defaults: [20, true]
|
||||
};
|
||||
|
||||
this.blocks.tieStitch =
|
||||
{
|
||||
only: SpriteMorph,
|
||||
|
@ -1812,7 +2018,6 @@ SpriteMorph.prototype.initBlocks = function () {
|
|||
};
|
||||
};
|
||||
|
||||
SpriteMorph.prototype.initBlocks();
|
||||
|
||||
// SpriteMorph block templates
|
||||
|
||||
|
@ -2033,9 +2238,15 @@ SpriteMorph.prototype.blockTemplates = function (category) {
|
|||
blocks.push(block('clear'));
|
||||
blocks.push('-');
|
||||
blocks.push(block('runningStitch'));
|
||||
blocks.push(block('beanStitch'));
|
||||
blocks.push(block('crossStitch'));
|
||||
blocks.push('-');
|
||||
blocks.push(block('zigzagStitch'));
|
||||
blocks.push(block('satinStitch'));
|
||||
blocks.push('-');
|
||||
blocks.push(block('jumpStitch'));
|
||||
blocks.push(block('tieStitch'));
|
||||
|
||||
blocks.push('-');
|
||||
|
||||
} else if (cat === 'colors') {
|
||||
blocks.push(block('setColor'));
|
||||
|
|
Ładowanie…
Reference in New Issue