Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

ContinuousDash.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
_global.ContinuousDash = function(Movieclip_to_draw_in, Length_of_dash, Length_of_space, lineStyle_ary, gapStyle_ary){
	this.setMovieClip(Movieclip_to_draw_in);
	this.setDash(Length_of_dash, Length_of_space);
	this.isLine = true;
	this.oLen = 0;
	this.lastTo = {x:0, y:0};
	this.styles = new Array();
	this.setStyles(lineStyle_ary, gapStyle_ary);
}
ContinuousDash.dist = function(x1, y1, x2, y2) {
	if (arguments.Len == 2) return Math.sqrt(x1*x1 + y1*y1);
	var dx = x2 - x1, dy = y2 - y1;
	return Math.sqrt(dx*dx + dy*dy);
}
ContinuousDash.prototype.setDash = function(Length_of_dash, Length_of_space){
	if (Length_of_dash) this.dLen = Length_of_dash;
	if (Length_of_space) this.sLen = Length_of_space;
	this.isLine = true;
	this.oLen = 0;
	return this;
}
ContinuousDash.prototype.setStartOffset = function(offset){
	var pLen = this.dLen + this.sLen;
	offset -= Math.floor(offset/pLen)*pLen;
	this.isLine = (this.dLen > offset);
	if (!this.isLine) offset -= this.dLen;
	if (!offset) this.isLine = !this.isLine;
	this.oLen = offset;
}
ContinuousDash.prototype.setMovieClip = function(Movieclip_to_draw_in){
	this.mc = Movieclip_to_draw_in;
	return this;
}
ContinuousDash.prototype.beginFill = function(){
	this.mc.beginFill.apply(this.mc, arguments);	
	return this;
}
ContinuousDash.prototype.endFill = function(){
	this.mc.endFill.apply(this.mc, arguments);
	return this;
}
ContinuousDash.prototype.lineStyle = function(thick, col, alpha){
	this.styles[0] = [thick, col, alpha];
	this.mc.lineStyle.apply(this.mc, arguments);
	return this;
}
ContinuousDash.prototype.gapStyle = function(thick, col, alpha){
	this.styles[1] = [thick, col, alpha];
	return this;
}
ContinuousDash.prototype.setStyles = function(lineStyle_ary, gapStyle_ary){
	if (lineStyle_ary){
		this.mc.lineStyle.apply(this.mc, arguments);
		this.styles[0] = lineStyle_ary;
	}
	if (gapStyle_ary) this.styles[1] =  gapStyle_ary;
	return this;
}
ContinuousDash.prototype.clear = function(){
	this.mc.clear.apply(this.mc, arguments);
	return this;
}
ContinuousDash.prototype.moveTo = function(x,y){
	this.lastTo = {x:x, y:y};
	this.mc.moveTo(x, y);	
	return this;
}
ContinuousDash.prototype.gapTo = function(x,y){
	if (x == this.lastTo.x && y == this.lastTo.y) return this;
	if (!this.styles[1]) return this.moveTo(x,y);
	this.lastTo = {x:x, y:y};
	this.mc.lineStyle.apply(this.mc, this.styles[1]);
	this.mc.lineTo(x, y);	
	return this;
}
ContinuousDash.prototype._lineTo = function(x,y){
	if (x == this.lastTo.x && y == this.lastTo.y) return this;
	this.lastTo = {x:x, y:y};
	if (this.styles[1]) this.mc.lineStyle.apply(this.mc, this.styles[0]);
	this.mc.lineTo(x, y);
	return this;
}
ContinuousDash.prototype.lineTo = function(x,y){
	var dx = x-this.lastTo.x,	dy = y-this.lastTo.y;
	var a = Math.atan2(dy,dx);
	var ca = Math.cos(a),	sa = Math.sin(a);
	var cLen = ContinuousDash.dist(dx,dy);
	if (this.oLen){
		if (this.oLen > cLen){
			if (this.isLine) this._lineTo(x,y);
			else this.gapTo(x,y);
			this.oLen -= cLen;
			return this;
		}else{
			if (this.isLine) this._lineTo(this.lastTo.x + ca*this.oLen, this.lastTo.y + sa*this.oLen);
			else this.gapTo(this.lastTo.x + ca*this.oLen, this.lastTo.y + sa*this.oLen);
			cLen -= this.oLen;
			this.oLen = 0;
			this.isLine = !this.isLine;
			if (!cLen) return this;
		}
	}
	var pLen = this.dLen + this.sLen;
	var pNum = Math.floor(cLen/pLen);
	if (pNum){
		var lx = ca*this.dLen,	ly = sa*this.dLen;
		var sx = ca*this.sLen,	sy = sa*this.sLen;
		for (var i=0; i<pNum; i++){
			if (this.isLine){
				this._lineTo(this.lastTo.x+lx, this.lastTo.y+ly);
				this.gapTo(this.lastTo.x+sx, this.lastTo.y+sy);
			}else{
				this.gapTo(this.lastTo.x+sx, this.lastTo.y+sy);
				this._lineTo(this.lastTo.x+lx, this.lastTo.y+ly);
			}
		}
		cLen -= pLen*pNum;
	}
	if (this.isLine){
		if (cLen > this.dLen){
			this._lineTo(this.lastTo.x+ca*this.dLen, this.lastTo.y+sa*this.dLen);
			this.oLen = this.sLen-(cLen-this.dLen);
			this.isLine = false;
			this.gapTo(x,y);
		}else{
			this._lineTo(x,y);
			if (cLen == this.dLen){
				this.oLen = 0;
				this.isLine = !this.isLine;
			}else{
				this.oLen = this.dLen-cLen;
				this.gapTo(x,y);
			}
		}
	}else{
		if (cLen > this.sLen){
			this.gapTo(this.lastTo.x+ca*this.sLen, this.lastTo.y+sa*this.sLen);
			this._lineTo(x, y);
			this.oLen = this.dLen-(cLen-this.sLen);
			this.isLine = true;
		}else{
			this.gapTo(x,y);
			if (cLen == this.sLen){
				this.oLen = 0;
				this.isLine = !this.isLine;
			}else this.oLen = this.sLen-cLen;
		}
	}
	return this;
}