Skip to content

Commit 72b23c5

Browse files
committed
carreQuiErre fondColore
1 parent 9951cb2 commit 72b23c5

10 files changed

+280
-99
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"test": "tests"
1313
},
1414
"scripts": {
15-
"test": "node test/testImageDataUtils.js",
15+
"test": "node test/testSuite.js",
1616
"start": "node server.js"
1717
},
1818
"repository": {

public/animations/carreQuiErre.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
define({
2+
draw: function (context, borders){
3+
context.fillStyle = "rgb(100,250,0)";
4+
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
5+
6+
context.save();
7+
context.translate(this.halfWidth, this.halfHeight);
8+
context.scale(3, 3);
9+
context.rotate(this.rotation* this.toRadians);
10+
11+
this.rotation = this.rotation + 1;
12+
13+
context.fillStyle = "rgb(150,20,200)";
14+
context.fillRect(-25, -25, 50, 50);
15+
16+
context.restore();
17+
},
18+
setup: function (context){
19+
this.toRadians = Math.PI / 180;
20+
21+
this.rotation = 0;
22+
this.halfWidth = context.canvas.width / 2;
23+
this.halfHeight = context.canvas.height / 2;
24+
}});

public/animations/fondColore.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
define(["bibs/wanderingPoint"], function(wp){ return {
2+
setup: function (context){
3+
var startPoint = [0, 0,0];
4+
var limit = [0 , 255 ];
5+
var limits = [limit, limit, limit];
6+
var speed = 2 ;
7+
var direction = [1, 3, 1];
8+
this.w = wp.makeWanderer(startPoint, direction, speed, limits);
9+
10+
},
11+
draw: function (context, borders){
12+
this.w.move();
13+
var red = Math.round(this.w.coordinates[0]);
14+
var green = Math.round(this.w.coordinates[1]);
15+
var blue = Math.round(this.w.coordinates[2]);
16+
context.fillStyle = "rgb("+ red +","+ green +","+blue +")";
17+
context.fillRect(0, 0, context.canvas.width, context.canvas.height);
18+
}
19+
}
20+
});

public/animations/rot.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
define(["bibs/canvasBuffer"], function(canvasBuffer){ return {
22
setup: function(context){
33
this.TO_RADIANS = Math.PI/180;
4-
this.buffer = canvasBuffer.makeBuffer(context,
5-
context.canvas.width,
4+
this.buffer = canvasBuffer.makeBuffer(context.canvas.width,
65
context.canvas.height);
76
this.axis = {x: 0, y: 20};
87
this.rotation = 0;
@@ -16,14 +15,14 @@ define(["bibs/canvasBuffer"], function(canvasBuffer){ return {
1615
context.putImageData(borders.south, 0, context.canvas.height -1 );
1716

1817
//copy canvas to buffer
19-
this.buffer.copyToBuffer({x:0, y:0});
18+
this.buffer.copyToBuffer(context, {x:0, y:0});
2019

2120
//copy from buffer to rotated canvas
2221
context.save();
2322
context.translate(this.axis.x, this.axis.y);
2423
context.rotate(this.rotation * this.TO_RADIANS);
2524
context.translate(-this.axis.x, -this.axis.y);
26-
this.buffer.copyFromBuffer();
25+
this.buffer.copyFromBuffer(context);
2726
context.restore();
2827

2928
// change rotation for next loop

public/bibs/canvasBuffer.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
define(function(){
22

3-
var makeBuffer = function(originalCtx, width, height){
3+
var makeBuffer = function(width, height){
44
var buffer = document.createElement('canvas');
55
buffer.width = width;
66
buffer.height = height;
77
var bufferCtx = buffer.getContext("2d");
88

99
return {
10-
width: buffer.width,
11-
height: buffer.height,
12-
copyToBuffer: function(sourcePoint){
10+
width: width,
11+
height: height,
12+
copyToBuffer: function(sourceCtx, sourcePoint){
13+
// Copy a rectangle the size of the buffer from sourcePoint.
14+
// This function does not react to translate, rotate etc..
1315
var x = Math.round(sourcePoint.x);
1416
var y = Math.round(sourcePoint.y);
15-
var imageData = originalCtx.getImageData(sourcePoint.x,
16-
sourcePoint.y,
17-
buffer.width,
18-
buffer.height);
17+
var imageData = sourceCtx.getImageData(x, y, width, height);
1918
bufferCtx.putImageData(imageData, 0, 0);
2019
},
21-
copyFromBuffer: function(destRec){
22-
// use drawImage because it allows to scale
23-
// and translate originalCtx
24-
originalCtx.drawImage(buffer,
25-
0, 0,
26-
buffer.width,
27-
buffer.height);
20+
copyFromBuffer: function(destinationCtx){
21+
// use drawImage because it allows to scale,
22+
// translate and rotate destinationCtx
23+
destinationCtx.drawImage(buffer, 0, 0, width, height);
2824
}
2925
};
3026
};
3127

32-
return { makeBuffer: makeBuffer};
28+
return { makeBuffer: makeBuffer };
3329
});

public/bibs/wanderingPoint.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
define(function(){
2+
var norm = function(vector){
3+
var sumOfSquares = 0;
4+
vector.forEach(function(el){ sumOfSquares += el * el; });
5+
return Math.sqrt(sumOfSquares);
6+
};
7+
8+
var normalize = function(vector){
9+
var length = norm(vector);
10+
return vector.map(function(el){ return el / length; });
11+
};
12+
13+
var movePoint = function(startPoint, direction, speed, fractionToTravel){
14+
return startPoint.map(function(coord, index){
15+
return coord + direction[index] * speed * fractionToTravel ;
16+
});
17+
};
18+
19+
var measureCompletion = function(startPoint, nextPointUnconstrained, limits)
20+
{
21+
var possibleImpacts = nextPointUnconstrained.map(
22+
function(coord,dimension){
23+
var newCoord = coord;
24+
if(coord < limits[dimension][0]){
25+
newCoord = limits[dimension][0];
26+
}else if(coord > limits[dimension][1]){
27+
newCoord = limits[dimension][1];
28+
}
29+
var toTravel = coord - startPoint[dimension];
30+
var travelled = newCoord - startPoint[dimension];
31+
var fraction = toTravel != 0 ? travelled / toTravel : 1;
32+
return [fraction, dimension];
33+
});
34+
var closestPossibleImpact = possibleImpacts.sort()[0];
35+
return {percentage: closestPossibleImpact[0],
36+
limitingDimension: closestPossibleImpact[1] };
37+
};
38+
39+
var next = function(startPoint, direction, speed, limits){
40+
var totalTravelledPercentage = 0;
41+
var nextPoint = null;
42+
direction = direction.slice();
43+
44+
while (totalTravelledPercentage < 0.999999){
45+
if(nextPoint){
46+
direction[completion.limitingDimension] *= -1;
47+
startPoint = nextPoint;
48+
}
49+
var remainingDist = 1 - totalTravelledPercentage;
50+
var nextUnconstrained = movePoint(startPoint, direction, speed,
51+
remainingDist);
52+
var completion = measureCompletion(startPoint, nextUnconstrained,
53+
limits);
54+
var additionalDist = remainingDist * completion.percentage;
55+
nextPoint = movePoint(startPoint, direction, speed, additionalDist);
56+
totalTravelledPercentage += additionalDist;
57+
};
58+
59+
return {point: nextPoint, direction: direction};
60+
};
61+
62+
var makeWanderer = function(startPoint, direction, speed, limits){
63+
return {
64+
coordinates: startPoint,
65+
limits: limits,
66+
direction: normalize(direction),
67+
speed: speed,
68+
move: function(){
69+
var nextState = next(this.coordinates, this.direction,
70+
this.speed, this.limits);
71+
this.coordinates = nextState.point;
72+
this.direction = nextState.direction;
73+
}
74+
};
75+
};
76+
77+
return {_norm: norm,
78+
_normalize: normalize,
79+
_next: next,
80+
makeWanderer: makeWanderer
81+
};
82+
});

public/exquis.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ define(["iter2d", "csshelper", "evileval", "net", "ui"], function(iter2d, csshel
6262
var codeAsUri = this.toDataUri(codeString);
6363
evileval.loadJsAnim(codeAsUri)
6464
.then(function(evaluatedAnimationClone){
65-
this.setAnimation(evaluatedAnimationClone, this.originalUrl);
65+
this.setAnimation(evaluatedAnimationClone,
66+
this.originalUrl);
6667
this.codeCacheUri = codeAsUri;
6768
resolve();
6869
}.bind(this))
@@ -73,13 +74,16 @@ define(["iter2d", "csshelper", "evileval", "net", "ui"], function(iter2d, csshel
7374
};
7475
}.bind(this));
7576
},
77+
7678
loadAnim: function(url){
77-
return evileval.loadJsAnim(url).then(function(evaluatedAnimationClone){
78-
this.setAnimation(evaluatedAnimationClone, url);
79-
this.codeCacheUri = null;
80-
return this;
81-
}.bind(this));
79+
return evileval.loadJsAnim(url).then(
80+
function(evaluatedAnimationClone){
81+
this.setAnimation(evaluatedAnimationClone, url);
82+
this.codeCacheUri = null;
83+
return this;
84+
}.bind(this));
8285
},
86+
8387
setAnimation: function(codeToSetup, uri){
8488
this.codeToSetup = codeToSetup;
8589
this.animationName = net.extractAnimationNameFromUri(uri),

public/tests/clientTest.js

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
"use strict";
2+
3+
4+
5+
define(["/lib/jquery-2.0.2.min.js", "/lib/async.js"], function(jq, async){
6+
var isFunction = function(functionToCheck) {
7+
var getType = {};
8+
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
9+
};
10+
11+
var testsDefinitions = [
12+
{ name : "checkHomepage",
13+
message: "The title should be Exquis",
14+
assertion: document.title == "Exquis"
15+
},
16+
{ name: "editorIsNotVisibleOnStartUp",
17+
message: "The editor should not be visible",
18+
assertion: $("#editor").hasClass("invisible")
19+
},
20+
{ name: "nineCanvasElements",
21+
message: "we should have 9 canvas elements",
22+
assertion: $("canvas").length == 9
23+
},
24+
function(){
25+
$("#hint-2-1").click();
26+
27+
var results = [
28+
{ name: "onCanvasClickEditorVisible",
29+
message: "on canvas click editor should be visible",
30+
assertion: ! $("#editor").hasClass("invisible")
31+
}];
32+
$("#editor").addClass("invisible");
33+
34+
return results;
35+
}
36+
];
37+
38+
var asyncTests = [
39+
function(done, net){
40+
41+
var savedAnimation,
42+
fileName,
43+
originalNetSaveAnimation = net.saveAnimation;
44+
net.saveAnimation = function(cell, callback, _fileName){
45+
console.log(_fileName);
46+
savedAnimation = cell.animation.drawString;
47+
fileName = _fileName;
48+
net.saveAnimation = originalNetSaveAnimation;
49+
};
50+
var correctFileName = "someTestAnim";
51+
// var codeString = "var i = 0;";
52+
// $("#hint-2-1").click();
53+
// $("#ace > textarea").val(codeString);
54+
// $("#ace > textarea").keyup();
55+
setTimeout(function(){
56+
$("#animation_save_as_button").click();
57+
$("#prompt_input").value = correctFileName;
58+
$("#ok_button").click();
59+
60+
var result = [
61+
// {name : "onSaveAsClick",
62+
// message: codeString + " != " + savedAnimation,
63+
// assertion: codeString === savedAnimation
64+
// },
65+
{name: "onSaveAsClick",
66+
message: "file name should be " + correctFileName + " not " + fileName,
67+
assertion: fileName === correctFileName
68+
}];
69+
done(null, result);
70+
}, 1000);
71+
72+
}
73+
74+
];
75+
76+
77+
78+
var test = function(net){
79+
var failureCount = 0,
80+
testCount = 0,
81+
tests;
82+
83+
84+
tests = testsDefinitions.reduce(function(accum, nextValue){
85+
if (isFunction(nextValue)){
86+
var asserts = nextValue(net);
87+
accum = accum.concat(asserts);
88+
}else{
89+
accum.push(nextValue);
90+
}
91+
92+
return accum;
93+
}, []);
94+
95+
96+
97+
async.mapSeries(asyncTests, function(val, cb){
98+
val(cb, net);
99+
}, function(err, results){
100+
var flatResults = results.reduce(function(a, b) {
101+
return a.concat(b);
102+
});
103+
104+
var allResults = tests.concat(flatResults);
105+
106+
allResults.forEach(function(test){
107+
108+
testCount ++;
109+
110+
if (!test.assertion){
111+
failureCount ++;
112+
console.log(test.message);
113+
};
114+
});
115+
116+
console.log(failureCount + " failures out of " +testCount + " tests.");
117+
});
118+
119+
120+
};
121+
122+
123+
return {
124+
test: test
125+
};
126+
});

0 commit comments

Comments
 (0)