Skip to content

Commit fac6f96

Browse files
author
cdris
committed
start new animation
1 parent 90bce47 commit fac6f96

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

js/weave.js

+19-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
const WIDTH = 800;
22
const HEIGHT = 600;
3+
const PAPER = Snap(WIDTH, HEIGHT);
4+
var activeWeft = null;
35

46
function weave() {
57
console.log("render!");
@@ -37,6 +39,10 @@ function weave() {
3739

3840
var reader = new FileReader();
3941
reader.onload = function() {
42+
if (activeWeft != null) {
43+
activeWeft.stop();
44+
}
45+
PAPER.clear();
4046
renderWeave(heddles, width,
4147
generateBinary(this.result), this.result.byteLength);
4248
}
@@ -54,19 +60,18 @@ function* generateBinary(buf) {
5460
}
5561

5662
function renderWeave(heddles, width, binary, byteLength) {
57-
var s = Snap(WIDTH, HEIGHT);
5863
var yarnWidth = WIDTH / ((width * 2) + 1);
59-
var warp = generateWarp(s, width);
60-
var weftPath = generateWeftPath(s, width, heddles, byteLength);
64+
var warp = generateWarp(width);
65+
var weftPath = generateWeftPath(width, heddles, byteLength);
6166
var weftLength = Snap.path.getTotalLength(weftPath);
62-
var weft = s.path({
67+
var weft = PAPER.path({
6368
path: Snap.path.getSubpath(weftPath, 0, 0),
6469
stroke: "#c00",
6570
strokeWidth: yarnWidth,
6671
strokeLinecap: "round",
6772
fillOpacity: 0
6873
});
69-
var mask = generateMask(s, width, heddles, binary);
74+
var mask = generateMask(width, heddles, binary);
7075

7176
console.log('starting stroke animation');
7277
Snap.animate(0, weftLength,
@@ -81,12 +86,12 @@ function renderWeave(heddles, width, binary, byteLength) {
8186
function(){ console.log("done"); }); //callback
8287
}
8388

84-
function generateWarp(s, width) {
85-
var warp = s.g();
89+
function generateWarp(width) {
90+
var warp = PAPER.g();
8691
var yarnWidth = WIDTH / ((width * 2) + 1);
8792
for (var i = 0; i < width; i++) {
8893
var x = yarnWidth * (i*2 + 1.5);
89-
var line = s.line(x, 0, x, HEIGHT);
94+
var line = PAPER.line(x, 0, x, HEIGHT);
9095
line.attr({
9196
stroke: "#bada55",
9297
strokeWidth: yarnWidth
@@ -96,7 +101,7 @@ function generateWarp(s, width) {
96101
return warp;
97102
}
98103

99-
function generateWeftPath(s, width, heddles, byteLength) {
104+
function generateWeftPath(width, heddles, byteLength) {
100105
var numRows = Math.ceil((byteLength * 8) / heddles);
101106
var yarnWidth = WIDTH / ((width * 2) + 1);
102107
var path = `M 0 ${HEIGHT - (yarnWidth * 1.5)}`;
@@ -106,22 +111,11 @@ function generateWeftPath(s, width, heddles, byteLength) {
106111
path += ` L ${x} ${y} L ${x} ${y - (yarnWidth * 2)}`;
107112
}
108113
return path;
109-
/*
110-
var weft = s.path({
111-
//path: path,
112-
path: Snap.path.getSubpath(path, 0, 0),
113-
stroke: "#c00",
114-
strokeWidth: yarnWidth,
115-
strokeLinecap: "round",
116-
fillOpacity: 0
117-
});
118-
return { weft: weft, length: Snap.path.getTotalLength(path) };
119-
*/
120114
}
121115

122-
function generateMask(s, width, heddles, binary) {
116+
function generateMask(width, heddles, binary) {
123117
var yarnWidth = WIDTH / ((width * 2) + 1);
124-
var mask = s.g();
118+
var mask = PAPER.g();
125119
var y = HEIGHT - (yarnWidth * 1.5);
126120
var i = 0;
127121
var p = ""
@@ -130,7 +124,7 @@ function generateMask(s, width, heddles, binary) {
130124
if (bit == 1) {
131125
for (var j = i % heddles; j < width; j += heddles) {
132126
var x = yarnWidth + (j * yarnWidth * 2);
133-
line = s.line(x, y, x + yarnWidth, y);
127+
line = PAPER.line(x, y, x + yarnWidth, y);
134128
line.attr({
135129
stroke: "#bada55",
136130
strokeWidth: yarnWidth + 1
@@ -141,7 +135,7 @@ function generateMask(s, width, heddles, binary) {
141135
i++;
142136
if (i % heddles == 0) {
143137
y -= yarnWidth * 2;
144-
//console.log(p);
138+
console.log(p);
145139
p = "";
146140
}
147141
}
@@ -152,8 +146,8 @@ function getCurve(x1, y1, x2, y2, sweep) {
152146
return `M ${x1} ${y1} A 1 1 0 0 ${sweep} ${x2} ${y2}`;
153147
}
154148

155-
console.log(getCurve(1,2,3,4,1));
156149
/*
150+
console.log(getCurve(1,2,3,4,1));
157151
var loop = "M200 400 A 1 1 0 0 1 600 200";
158152
159153
var loopLength = Snap.path.getTotalLength(loop);

0 commit comments

Comments
 (0)