1
1
const WIDTH = 800 ;
2
2
const HEIGHT = 600 ;
3
+ const PAPER = Snap ( WIDTH , HEIGHT ) ;
4
+ var activeWeft = null ;
3
5
4
6
function weave ( ) {
5
7
console . log ( "render!" ) ;
@@ -37,6 +39,10 @@ function weave() {
37
39
38
40
var reader = new FileReader ( ) ;
39
41
reader . onload = function ( ) {
42
+ if ( activeWeft != null ) {
43
+ activeWeft . stop ( ) ;
44
+ }
45
+ PAPER . clear ( ) ;
40
46
renderWeave ( heddles , width ,
41
47
generateBinary ( this . result ) , this . result . byteLength ) ;
42
48
}
@@ -54,19 +60,18 @@ function* generateBinary(buf) {
54
60
}
55
61
56
62
function renderWeave ( heddles , width , binary , byteLength ) {
57
- var s = Snap ( WIDTH , HEIGHT ) ;
58
63
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 ) ;
61
66
var weftLength = Snap . path . getTotalLength ( weftPath ) ;
62
- var weft = s . path ( {
67
+ var weft = PAPER . path ( {
63
68
path : Snap . path . getSubpath ( weftPath , 0 , 0 ) ,
64
69
stroke : "#c00" ,
65
70
strokeWidth : yarnWidth ,
66
71
strokeLinecap : "round" ,
67
72
fillOpacity : 0
68
73
} ) ;
69
- var mask = generateMask ( s , width , heddles , binary ) ;
74
+ var mask = generateMask ( width , heddles , binary ) ;
70
75
71
76
console . log ( 'starting stroke animation' ) ;
72
77
Snap . animate ( 0 , weftLength ,
@@ -81,12 +86,12 @@ function renderWeave(heddles, width, binary, byteLength) {
81
86
function ( ) { console . log ( "done" ) ; } ) ; //callback
82
87
}
83
88
84
- function generateWarp ( s , width ) {
85
- var warp = s . g ( ) ;
89
+ function generateWarp ( width ) {
90
+ var warp = PAPER . g ( ) ;
86
91
var yarnWidth = WIDTH / ( ( width * 2 ) + 1 ) ;
87
92
for ( var i = 0 ; i < width ; i ++ ) {
88
93
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 ) ;
90
95
line . attr ( {
91
96
stroke : "#bada55" ,
92
97
strokeWidth : yarnWidth
@@ -96,7 +101,7 @@ function generateWarp(s, width) {
96
101
return warp ;
97
102
}
98
103
99
- function generateWeftPath ( s , width , heddles , byteLength ) {
104
+ function generateWeftPath ( width , heddles , byteLength ) {
100
105
var numRows = Math . ceil ( ( byteLength * 8 ) / heddles ) ;
101
106
var yarnWidth = WIDTH / ( ( width * 2 ) + 1 ) ;
102
107
var path = `M 0 ${ HEIGHT - ( yarnWidth * 1.5 ) } ` ;
@@ -106,22 +111,11 @@ function generateWeftPath(s, width, heddles, byteLength) {
106
111
path += ` L ${ x } ${ y } L ${ x } ${ y - ( yarnWidth * 2 ) } ` ;
107
112
}
108
113
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
- */
120
114
}
121
115
122
- function generateMask ( s , width , heddles , binary ) {
116
+ function generateMask ( width , heddles , binary ) {
123
117
var yarnWidth = WIDTH / ( ( width * 2 ) + 1 ) ;
124
- var mask = s . g ( ) ;
118
+ var mask = PAPER . g ( ) ;
125
119
var y = HEIGHT - ( yarnWidth * 1.5 ) ;
126
120
var i = 0 ;
127
121
var p = ""
@@ -130,7 +124,7 @@ function generateMask(s, width, heddles, binary) {
130
124
if ( bit == 1 ) {
131
125
for ( var j = i % heddles ; j < width ; j += heddles ) {
132
126
var x = yarnWidth + ( j * yarnWidth * 2 ) ;
133
- line = s . line ( x , y , x + yarnWidth , y ) ;
127
+ line = PAPER . line ( x , y , x + yarnWidth , y ) ;
134
128
line . attr ( {
135
129
stroke : "#bada55" ,
136
130
strokeWidth : yarnWidth + 1
@@ -141,7 +135,7 @@ function generateMask(s, width, heddles, binary) {
141
135
i ++ ;
142
136
if ( i % heddles == 0 ) {
143
137
y -= yarnWidth * 2 ;
144
- // console.log(p);
138
+ console . log ( p ) ;
145
139
p = "" ;
146
140
}
147
141
}
@@ -152,8 +146,8 @@ function getCurve(x1, y1, x2, y2, sweep) {
152
146
return `M ${ x1 } ${ y1 } A 1 1 0 0 ${ sweep } ${ x2 } ${ y2 } ` ;
153
147
}
154
148
155
- console . log ( getCurve ( 1 , 2 , 3 , 4 , 1 ) ) ;
156
149
/*
150
+ console.log(getCurve(1,2,3,4,1));
157
151
var loop = "M200 400 A 1 1 0 0 1 600 200";
158
152
159
153
var loopLength = Snap.path.getTotalLength(loop);
0 commit comments