forked from kamicane/art
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathpath.js
60 lines (39 loc) · 1.59 KB
/
path.js
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
require('../mode');
var ART = require('../../index');
var MetricsPath = require('../../metrics/path');
var SVGPath = require('../../modes/svg/path');
var art = new ART.Surface(1000, 600);
var group = new ART.Group();
var curve = new ART.Path().move(200, 20).curve(100, 0, 0, 200, 300, 300);
curve.move(-100, -100).line(100, -100).line(100, 100);
curve.arc(-100, 0, 100, 200).counterArc(0, 50, 100);
var line = new ART.Shape(curve).stroke('#00F', 1).translate(0,0).inject(group);
group.inject(art);
group.scale(1, 1);
var curve = new MetricsPath()
curve._measureLine = curve.onLine;
curve.onLine = function(sx, sy, x, y){
this._measureLine(sx, sy, x, y);
new ART.Rectangle(3, 3).fill('#0f0').move(x - 1, y - 1).inject(group);
};
curve.move(200, 20).curve(100, 0, 0, 200, 300, 300)
.move(-100, -100).line(100, -100).line(100, 100)
.arc(-100, 0, 100, 200).counterArc(0, 50, 100);
var square = new ART.Rectangle(20, 10).fill('#F00').inject(group);
art.inject(document.body);
console.log(curve.length);
var curve2 = new SVGPath()
.move(200, 20).curve(100, 0, 0, 200, 300, 300)
.move(-100, -100).line(100, -100).line(100, 100)
.arc(-100, 0, 100, 200).counterArc(0, 50, 100);
if (document.createElementNS){
var nativeCurve = document.createElementNS('http://www.w3.org/2000/svg', 'path');
nativeCurve.setAttribute('d', curve2.toSVG());
console.log(nativeCurve.getTotalLength());
}
var l = curve.length;
setInterval(function(){
var i = ((+new Date()) / 50) % l;
var point = curve.point(i);
square.transformTo(point).translate(-15, -5);
}, 50);