Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Artist draw pattern backward #20989

Merged
merged 9 commits into from
Mar 5, 2018
34 changes: 24 additions & 10 deletions apps/src/turtle/turtle.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@ class Visualization {
this.turtleFrame_ = 0;
}

clipImage() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you didn't end up using this function, so you can remove it.


}

// Helper for creating canvas elements.
createCanvas_(id, width, height) {
var el = document.createElement('canvas');
Expand Down Expand Up @@ -547,15 +551,24 @@ class Visualization {
// Need to subtract 90 to accommodate difference in canvas vs. Turtle direction
this.ctxScratch.rotate(this.degreesToRadians_(this.heading - 90));

if (img.width !== 0) {
this.ctxScratch.drawImage(img,
// Start point for clipping image
0, 0,
// clip region size
distance + img.height / 2, img.height,
// draw location relative to the ctx.translate point pre-rotation
-img.height / 4, -img.height / 2,
distance+img.height / 2, img.height);
if ((distance > 0) && (img.width !== 0)) {
this.ctxScratch.drawImage(img,
// Start point for clipping image forward
0, 0,
// clip region size
distance, img.height,
// draw location relative to the ctx.translate point pre-rotation
-img.height / 4, -img.height / 2,
distance+img.height / 2, img.height);
} else if ((distance < 0) && (img.width !== 0)) {
this.ctxScratch.drawImage(img,
// Start point for clipping image backward
img.width, 0,
// clip region size
distance, img.height,
// draw location relative to the ctx.translate point pre-rotation
-img.height / 4, -img.height / 2,
distance+img.height / 2, img.height);
}

this.ctxScratch.restore();
Expand Down Expand Up @@ -902,6 +915,7 @@ Artist.prototype.afterInject_ = function (config) {
var visualization = document.getElementById('visualization');
visualization.appendChild(this.visualization.displayCanvas);

// TODO (br-pair): - pull this out?
if (this.studioApp_.isUsingBlockly() && this.isFrozenSkin()) {
// Override colour_random to only generate random colors from within our frozen
// palette
Expand Down Expand Up @@ -1510,7 +1524,7 @@ Artist.prototype.step = function (command, values, options) {
result = this.calculateSmoothAnimate(options, distance);
tupleDone = result.tupleDone;
this.visualization.setHeading(heading);
this.visualization.jumpForward(result.distance);
this.jumpForward(result.distance);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changed with Josh's recent refactoring, it should be this.visualization.jumpForward(...

break;
case 'RT': // Right Turn
distance = values[0];
Expand Down
35 changes: 35 additions & 0 deletions apps/test/unit/turtle/turtleTest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sinon from 'sinon';
import {expect} from '../../util/configuredChai';
import {parseElement} from '@cdo/apps/xml';
const Artist = require('@cdo/apps/turtle/turtle');
Expand Down Expand Up @@ -55,6 +56,40 @@ describe('Artist', () => {
});
});

describe('drawing with patterns', () => {
it('draws a pattern backwards', () => {
let artist = new Artist();
let width = 100;
let height = 100;
let img = new Image(width, height);

artist.visualization = new Artist.Visualization();
artist.visualization.currentPathPattern = img;
const setDrawPatternBackwardSpy = sinon.spy(artist.visualization.ctxScratch, 'drawImage');
artist.visualization.drawForwardLineWithPattern_(-100);

expect(setDrawPatternBackwardSpy).to.be.have.been.calledWith(img, 100, 0, -100, 100, -25, -50, -50, 100);

setDrawPatternBackwardSpy.restore();
});

it('draws a pattern forward', () => {
let artist = new Artist();
let width = 100;
let height = 100;
let img = new Image(width, height);

artist.visualization = new Artist.Visualization();
artist.visualization.currentPathPattern = img;
const setDrawPatternForwardSpy = sinon.spy(artist.visualization.ctxScratch, 'drawImage');
artist.visualization.drawForwardLineWithPattern_(100);

expect(setDrawPatternForwardSpy).to.be.have.been.calledWith(img, 0, 0, 100, 100, -25, -50, 150, 100);

setDrawPatternForwardSpy.restore();
});
});

describe('jumpTo', () => {
let artist;
beforeEach(() => {
Expand Down