| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 // This code in inspired by the canvas lines test on the animometer |
| 5 // benchmark (https://trac.webkit.org/export/HEAD/trunk/PerformanceTests/Animome
ter/developer.html). |
| 6 // Javascript code: https://pr.gg/animometer/tests/master/resources/canvas-tests
.js |
| 7 |
| 8 function CanvasLine() { |
| 9 |
| 10 this.init = function(circle_x, circle_y, circle_radius, min_length, |
| 11 max_length, theta, omega, color, width) { |
| 12 this.theta = theta; // orientation angle |
| 13 this.omega = omega; // speed; oscillations/second |
| 14 |
| 15 this.color = color; |
| 16 |
| 17 this.min_length = min_length; |
| 18 this.max_length = max_length; |
| 19 |
| 20 this.x = circle_x + circle_radius * Math.cos(theta); |
| 21 this.y = circle_y + circle_radius * Math.sin(theta); |
| 22 this.direction = Math.floor(Math.random() * 2) * 2 - 1; // 1 or -1 |
| 23 |
| 24 this.width = width; |
| 25 this.init_time = Date.now(); |
| 26 } |
| 27 |
| 28 this.draw = function(context) { |
| 29 var seconds = (Date.now() - this.init_time) / 1000; |
| 30 var length = this.min_length + |
| 31 (this.max_length - this.min_length) * |
| 32 Math.abs(Math.sin(seconds * this.omega * 2 * Math.PI)); |
| 33 |
| 34 context.strokeStyle = this.color; |
| 35 context.lineWidth = this.width; |
| 36 |
| 37 context.beginPath(); |
| 38 context.moveTo(this.x, this.y); |
| 39 context.lineTo(this.x + this.direction * length * Math.cos(this.theta), |
| 40 this.y + this.direction * length * Math.sin(this.theta)); |
| 41 context.stroke(); |
| 42 } |
| 43 } |
| 44 |
| 45 var stage_lines = (function() { |
| 46 var initialized_lines; |
| 47 var width; |
| 48 var height; |
| 49 |
| 50 var circles_x; |
| 51 var circles_y; |
| 52 var circle_radius; |
| 53 |
| 54 var stage = {}; |
| 55 |
| 56 stage.init = function(number_lines, canvas_width, canvas_height) { |
| 57 width = canvas_width; |
| 58 height = canvas_height; |
| 59 initialized_lines = []; |
| 60 |
| 61 circle_radius = Math.min(canvas_width / 8, |
| 62 canvas_height / 8); |
| 63 circles_x = [canvas_width * 0.2, canvas_width * 0.4, |
| 64 canvas_width * 0.6, canvas_width * 0.8]; |
| 65 circles_y = [canvas_height * 0.3, canvas_height * 0.6, |
| 66 canvas_height * 0.3, canvas_height * 0.6]; |
| 67 |
| 68 circle_color = ["#e01040", "#10c030", "#744CBA", "#e05010"]; |
| 69 |
| 70 for(var i = 0; i < number_lines; i++) { |
| 71 var new_line = new CanvasLine(); |
| 72 |
| 73 var circle_number = Math.floor(Math.random() * circles_x.length); |
| 74 var theta = Math.random() * 2 * Math.PI; |
| 75 var omega = Math.random() / 2 + 0.2; |
| 76 var width = Math.random() * 8 + 1 |
| 77 var max_length = Math.random() * canvas_width / 15; |
| 78 var min_length = |
| 79 Math.min(Math.random() * canvas_width / 30, max_length /3) |
| 80 |
| 81 new_line.init(circles_x[circle_number], circles_y[circle_number], |
| 82 circle_radius, min_length, max_length, theta, omega, |
| 83 circle_color[circle_number], width); |
| 84 initialized_lines.push(new_line) |
| 85 } |
| 86 |
| 87 }; |
| 88 |
| 89 stage.draw = function(context) { |
| 90 // draw the circles |
| 91 for (var i = 0; i < circles_x.length; i++) { |
| 92 context.strokeStyle = circle_color[i]; |
| 93 context.beginPath(); |
| 94 context.arc(circles_x[i], circles_y[i], circle_radius, 0, 2 * Math.PI); |
| 95 context.stroke(); |
| 96 context.fillStyle = "#000000"; |
| 97 context.fill(); |
| 98 } |
| 99 |
| 100 // draw all initialized_lines on the context |
| 101 for(var i = 0; i < initialized_lines.length; i++) { |
| 102 initialized_lines[i].draw(context); |
| 103 } |
| 104 }; |
| 105 |
| 106 return stage; |
| 107 })(); |
| 108 |
| 109 |
| OLD | NEW |