PageSourceSearch

https://alloyteam.github.io/curvejs/asset/main.js

js alloyteam.github.io collected 2026-10-03 08:36:28 UTC 15,510 bytes, 594 lines download raw bytes

1(function (global, factory) {
2  typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
3  typeof define === 'function' && define.amd ? define(factory) :
4  (factory());
5}(this, (function () { 'use strict';
6
7var util = {
8
9    random: function random(min, max) {
10        return min + Math.floor(Math.random() * (max - min + 1));
11    },
12
13    randomColor: function randomColor() {
14        return ['#22CAB3', '#90CABE', '#A6EFE8', '#C0E9ED', '#C0E9ED', '#DBD4B7', '#D4B879', '#ECCEB2', '#F2ADA6', '#FF7784'][util.random(0, 9)];
15        // return '#'+(Math.random()*0xffffff<<0).toString(16);
16    },
17
18    randomSpeed: function randomSpeed() {
19        return (Math.random() > 0.5 ? 1 : -1) * Math.random() * 2;
20    }
21
22};
23
24(function () {
25    'use strict';
26
27    if (!Date.now) Date.now = function () {
28        return new Date().getTime();
29    };
30
31    var vendors = ['webkit', 'moz'];
32    for (var i = 0; i < vendors.length && !window.requestAnimationFrame; ++i) {
33        var vp = vendors[i];
34        window.requestAnimationFrame = window[vp + 'RequestAnimationFrame'];
35        window.cancelAnimationFrame = window[vp + 'CancelAnimationFrame'] || window[vp + 'CancelRequestAnimationFrame'];
36    }
37    if (/iP(ad|hone|od).*OS 6/.test(window.navigator.userAgent) // iOS6 is buggy
38    || !window.requestAnimationFrame || !window.cancelAnimationFrame) {
39        var lastTime = 0;
40        window.requestAnimationFrame = function (callback) {
41            var now = Date.now();
42            var nextTime = Math.max(lastTime + 16, now);
43            return setTimeout(function () {
44                callback(lastTime = nextTime);
45            }, nextTime - now);
46        };
47        window.cancelAnimationFrame = clearTimeout;
48    }
49})();
50
51/**
52 * dance motion, spin around.
53 *
54 * @param {points}
55 * @param {data}
56 *      data rule example:
57 *      { angle : 0, r : 5 , step : Math.PI / 50 }
58 */
59var dance = function (points, data) {
60    var pre = this.copyPoints,
61        theta = data.angle,
62        r = data.r;
63
64    var R = [[Math.cos(theta), -Math.sin(theta)], [Math.sin(theta), Math.cos(theta)]];
65    points[0] = pre[0] + R[0][0] * r;
66    points[1] = pre[1] + R[1][0] * r;
67    points[2] = pre[2] + R[0][0] * r;
68    points[3] = pre[3] + R[1][0] * r;
69    points[4] = pre[4] + R[0][0] * r;
70    points[5] = pre[5] + R[1][0] * r;
71    points[6] = pre[6] + R[0][0] * r;
72    points[7] = pre[7] + R[1][0] * r;
73
74    data.angle += data.step;
75};
76
77/**
78 * move motion.
79 *
80 * @param {points}
81 * @param {data}
82 *      data rule example:
83 *      [1, 0.2, -3, 0.7, 0.5, 0.3, -1, 1]
84 */
85var move = function (points, data) {
86    points.forEach(function (item, index) {
87        points[index] += data[index];
88    });
89};
90
91/**
92 * rotate motion.
93 *
94 * @param {points}
95 * @param {data}
96 *      data rule example:
97 *      Math.PI/100
98 */
99
100function _rotate(x1, y1, x2, y2, theta, index, points) {
101    var v = { x: x1 - x2, y: y1 - y2 };
102    var R = [[Math.cos(theta), -Math.sin(theta)], [Math.sin(theta), Math.cos(theta)]];
103
104    points[index] = x2 + R[0][0] * v.x + R[0][1] * v.y;
105    points[index + 1] = y2 + R[1][0] * v.x + R[1][1] * v.y;
106}
107
108function rotate(points, angle) {
109    var centerX = (points[0] + points[6]) / 2;
110    var centerY = (points[1] + points[7]) / 2;
111    _rotate(points[0], points[1], centerX, centerY, angle, 0, points);
112    _rotate(points[2], points[3], centerX, centerY, angle, 2, points);
113    _rotate(points[4], points[5], centerX, centerY, angle, 4, points);
114    _rotate(points[6], points[7], centerX, centerY, angle, 6, points);
115}
116
117function n(x, y) {
118    var sum = x * x + y * y;
119    if (sum === 0) return [0, 0];
120    var len = Math.sqrt(sum);
121    return [x / len, y / len];
122}
123
124function sl(x, y) {
125    return x * x + y * y;
126}
127
128var vector2 = {
129    n: n,
130    sl: sl
131};
132
133/**
134 * close or open path motion.
135 *
136 * @param {points}
137 */
138
139function open(points) {
140    var v = vector2.n(points[0] - points[6], points[1] - points[7]);
141
142    points[0] += v[0];
143    points[1] += v[1];
144
145    points[6] -= v[0];
146    points[7] -= v[1];
147}
148
149function close(points) {
150    var v = vector2.n(points[0] - points[6], points[1] - points[7]);
151
152    points[0] -= v[0];
153    points[1] -= v[1];
154
155    points[6] += v[0];
156    points[7] += v[1];
157}
158
159function auto(points) {}
160
161var path = {
162    close: close,
163    open: open,
164    auto: auto
165};
166
167function to(points, target) {
168    var va1 = [points[2] - points[0], points[3] - points[1]];
169    var va2 = [points[4] - points[2], points[5] - points[3]];
170    var va3 = [points[6] - points[4], points[7] - points[5]];
171
172    var vb1 = [target[2] - target[0], target[3] - target[1]];
173    var vb2 = [target[4] - target[2], target[5] - target[3]];
174    var vb3 = [target[6] - target[4], target[7] - target[5]];
175
176    var v1 = vector2.n(vb1[0] - va1[0], vb1[1] - va1[1]);
177    points[0] -= v1[0];
178    points[1] -= v1[1];
179
180    var v2 = vector2.n(vb2[0] - va2[0], vb2[1] - va2[1]);
181    points[4] += v2[0];
182    points[5] += v2[1];
183
184    var v3 = vector2.n(vb3[0] - va3[0], vb3[1] - va3[1]);
185    points[6] += v3[0];
186    points[7] += v3[1];
187}
188
189function line(arr) {
190
191    arr[2] += arr[2] > arr[0] ? -1 : 1;
192    arr[3] += arr[3] > arr[1] ? -1 : 1;
193    arr[4] += arr[4] > arr[6] ? -1 : 1;
194    arr[5] += arr[5] > arr[7] ? -1 : 1;
195}
196
197function circle(points, data) {
198    var R = [[Math.cos(data.angle), -Math.sin(data.angle)], [Math.sin(data.angle), Math.cos(data.angle)]];
199
200    this.data.angle += data.step;
201
202    points[0] = points[0] + R[0][0];
203    points[1] = points[1] + R[1][0];
204    points[6] = points[6] + R[0][0];
205    points[7] = points[7] + R[1][0];
206}
207
208var motion$1 = {
209    dance: dance,
210    move: move,
211    rotate: rotate,
212    to: to,
213    line: line,
214    circle: circle,
215    close: path.close,
216    open: path.open
217};
218
219var classCallCheck = function (instance, Constructor) {
220  if (!(instance instanceof Constructor)) {
221    throw new TypeError("Cannot call a class as a function");
222  }
223};
224
225var createClass = function () {
226  function defineProperties(target, props) {
227    for (var i = 0; i < props.length; i++) {
228      var descriptor = props[i];
229      descriptor.enumerable = descriptor.enumerable || false;
230      descriptor.configurable = true;
231      if ("value" in descriptor) descriptor.writable = true;
232      Object.defineProperty(target, descriptor.key, descriptor);
233    }
234  }
235
236  return function (Constructor, protoProps, staticProps) {
237    if (protoProps) defineProperties(Constructor.prototype, protoProps);
238    if (staticProps) defineProperties(Constructor, staticProps);
239    return Constructor;
240  };
241}();
242
243
244
245
246
247
248
249
250
251var inherits = function (subClass, superClass) {
252  if (typeof superClass !== "function" && superClass !== null) {
253    throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
254  }
255
256  subClass.prototype = Object.create(superClass && superClass.prototype, {
257    constructor: {
258      value: subClass,
259      enumerable: false,
260      writable: true,
261      configurable: true
262    }
263  });
264  if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
265};
266
267
268
269
270
271
272
273
274
275
276
277var possibleConstructorReturn = function (self, call) {
278  if (!self) {
279    throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
280  }
281
282  return call && (typeof call === "object" || typeof call === "function") ? call : self;
283};
284
285var Curve = function () {
286    function Curve(option) {
287        classCallCheck(this, Curve);
288
289        this.points = option.points || [];
290        this.color = option.color || 'black';
291        this.x = option.x || 0;
292        this.y = option.y || 0;
293        this.vision = option.vision || [];
294        this.visionMax = 640;
295        this.visionInterval = option.visionInterval || 10;
296
297        this._preDate = Date.now();
298        this._now = new Date();
299
300        this.data = option.data;
301
302        this.copyPoints = this.points.slice(0);
303        this.motion = option.motion || motion$1.dance;
304
305        this._initVision(option.visionCount || 80);
306    }
307
308    createClass(Curve, [{
309        key: '_initVision',
310        value: function _initVision() {
311            var i = 0;
312            for (; i < 80; i++) {
313                this.tick(true);
314            }
315        }
316    }, {
317        key: 'tick',
318        value: function tick(tickSelf) {
319            this._now = Date.now();
320            if (this._now - this._preDate > this.visionInterval || tickSelf) {
321
322                this.vision.push.apply(this.vision, this.points);
323                if (this.vision.length > this.visionMax) {
324                    this.vision.splice(0, 8);
325                }
326                this._preDate = this._now;
327            }
328
329            this.motion.call(this, this.points, this.data);
330        }
331    }, {
332        key: 'draw',
333        value: function draw(ctx) {
334            this.tick();
335            ctx.save();
336            ctx.translate(this.x, this.y);
337            ctx.globalAlpha = 1;
338            var points = this.points;
339            ctx.beginPath();
340            ctx.strokeStyle = this.color;
341            ctx.moveTo.call(ctx, points[0], points[1]);
342            ctx.bezierCurveTo.call(ctx, points[2], points[3], points[4], points[5], points[6], points[7]);
343            ctx.stroke();
344
345            var vision = this.vision;
346
347            var i = 0,
348                len = vision.length;
349            for (; i < len; i += 8) {
350                ctx.globalAlpha = i / this.visionMax * 0.1;
351                ctx.beginPath();
352                ctx.strokeStyle = this.color;
353                ctx.moveTo.call(ctx, vision[i], vision[i + 1]);
354                ctx.bezierCurveTo.call(ctx, vision[i + 2], vision[i + 3], vision[i + 4], vision[i + 5], vision[i + 6], vision[i + 7]);
355                ctx.stroke();
356            }
357            ctx.restore();
358        }
359    }, {
360        key: 'clone',
361        value: function clone() {}
362    }]);
363    return Curve;
364}();
365
366var Group = function () {
367    function Group() {
368        classCallCheck(this, Group);
369
370        this.children = [];
371    }
372
373    createClass(Group, [{
374        key: "add",
375        value: function add(line) {
376            this.children.push(line);
377        }
378    }, {
379        key: "remove",
380        value: function remove(line) {
381            var i = 0,
382                len = this.children.length;
383            for (; i < len; i++) {
384
385                if (line === this.children[i]) {
386
387                    this.children.splice(i, 1);
388                    break;
389                }
390            }
391        }
392    }, {
393        key: "draw",
394        value: function draw(ctx) {
395            this.children.forEach(function (child) {
396                child.draw(ctx);
397            });
398        }
399    }]);
400    return Group;
401}();
402
403var Stage$1 = function (_Group) {
404    inherits(Stage, _Group);
405
406    function Stage(width, height, renderTo) {
407        classCallCheck(this, Stage);
408
409        var _this = possibleConstructorReturn(this, (Stage.__proto__ || Object.getPrototypeOf(Stage)).call(this));
410
411        if (arguments.length === 1) {
412            _this.canvas = width;
413            _this.ctx = _this.canvas.getContext('2d');
414            _this.width = _this.canvas.width;
415            _this.height = _this.canvas.height;
416        } else if (arguments.length === 3) {
417            _this.canvas = document.createElement('canvas');
418            _this.canvas.width = width;
419            _this.canvas.height = height;
420            _this.width = width;
421            _this.height = height;
422            _this.ctx = _this.canvas.getContext('2d');
423            document.querySelector(renderTo).appendChild(_this.canvas);
424        }
425
426        return _this;
427    }
428
429    createClass(Stage, [{
430        key: 'addExistLine',
431        value: function addExistLine(process) {}
432    }, {
433        key: 'sameAs',
434        value: function sameAs(index) {}
435    }, {
436        key: 'update',
437        value: function update() {
438            var _this2 = this;
439
440            this.ctx.clearRect(0, 0, this.width, this.height);
441            this.children.forEach(function (child) {
442                child.draw(_this2.ctx);
443            });
444        }
445    }]);
446    return Stage;
447}(Group);
448
449/**
450 * Created by dntzhang on 2017/4/15.
451 */
452var data = {
453
454    'c': [[70, 70, 12, 76, 12, 123, 70, 128]],
455    'u': [[25, 68, 23, 140, 80, 140, 74, 100], [73, 62, 72, 90, 75, 110, 93, 134]],
456    'r': [[21, 62, 21, 62, 21, 128, 21, 128], [21, 128, 17, 96, 17, 68, 65, 60]],
457    'v': [[12, 58, 41, 132, 61, 132, 96, 58]],
458    'j': [[52, 48, 53, 48, 54, 49, 52, 48], [8, 175, 24, 204, 60, 197, 56, 74]],
459    'e': [[44, 104, 112, 52, -20, 82, 77, 136]], 's': [[81, 72, 17, 42, 105, 145, 41, 120]],
460    'l': [[77, 27, 30, 114, 36, 126, 57, 126]],
461    'i': [[56, 71, 56, 72, 59, 70, 56, 73], [47, 93, 59, 81, 47, 121, 59, 111]]
462
463};
464
465var Word$1 = function (_Group) {
466    inherits(Word, _Group);
467
468    function Word(word, option) {
469        classCallCheck(this, Word);
470
471        var _this = possibleConstructorReturn(this, (Word.__proto__ || Object.getPrototypeOf(Word)).call(this));
472
473        option = Object.assign({
474            x: 0, y: 0, color: 'black'
475        }, option);
476
477        _this.x = option.x;
478        _this.y = option.y;
479        _this.color = option.color;
480        _this.word = word;
481        _this.data = option.data;
482        _this.motion = option.motion;
483
484        _this.points = data[word];
485
486        _this._init();
487        return _this;
488    }
489
490    createClass(Word, [{
491        key: '_init',
492        value: function _init() {
493            var _this2 = this;
494
495            this.points.forEach(function (item) {
496
497                _this2.add(new Curve({
498                    x: _this2.x,
499                    y: _this2.y,
500                    points: item,
501                    color: _this2.color,
502                    data: _this2.data,
503                    motion: _this2.motion
504                }));
505            });
506        }
507    }]);
508    return Word;
509}(Group);
510
511var curvejs = {
512    Curve: Curve,
513    Group: Group,
514    Stage: Stage$1,
515    motion: motion$1,
516    Word: Word$1
517};
518
519var Stage = curvejs.Stage;
520var Word = curvejs.Word;
521var motion = curvejs.motion;
522
523
524var lineCount = 10;
525var random$1 = util.random;
526var randomColor$1 = util.randomColor;
527var randomSpeed$1 = util.randomSpeed;
528var stage = new Stage(500, 250, '#container');
529
530function generatePosition() {
531
532    stage.add(new Word('c', {
533        color: '#22CAB3',
534        motion: motion.dance,
535        data: { angle: 0, r: 5, step: Math.PI / 50 }
536
537    }));
538
539    stage.add(new Word('u', {
540        color: '#22CAB3',
541        x: 60,
542        motion: motion.dance,
543        data: { angle: 0, r: 5, step: Math.PI / 50 }
544    }));
545
546    stage.add(new Word('r', {
547        color: '#22CAB3',
548        x: 145,
549        motion: motion.dance,
550        data: { angle: 0, r: 5, step: Math.PI / 50 }
551    }));
552
553    stage.add(new Word('v', {
554        color: '#22CAB3',
555        x: 210,
556        y: 10,
557        motion: motion.dance,
558        data: { angle: 0, r: 5, step: Math.PI / 50 }
559    }));
560
561    stage.add(new Word('e', {
562        color: '#22CAB3',
563        x: 280,
564        y: -5,
565        motion: motion.dance,
566        data: { angle: 0, r: 5, step: Math.PI / 50 }
567    }));
568
569    stage.add(new Word('j', {
570        color: '#FF7784',
571        x: 350,
572        motion: motion.dance,
573        data: { angle: 0, r: 5, step: Math.PI / 50 }
574    }));
575
576    stage.add(new Word('s', {
577        color: '#FF7784',
578        x: 400,
579        motion: motion.dance,
580        data: { angle: 0, r: 5, step: Math.PI / 50 }
581    }));
582}
583
584function tick$1() {
585    stage.update();
586    requestAnimationFrame(tick$1);
587}
588
589(function main() {
590    generatePosition();
591    tick$1();
592})();
593
594})));

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.