| 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 stroke shapes 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/simple/resources/simple-canva
s-paths.js |
| 7 |
| 8 var shape_types = ["quadratic", "bezier", "arc_to", "arc", "rectangle", "ellipse
"]; |
| 9 |
| 10 function CanvasStrokeShape() { |
| 11 this.init = function(canvas_width, canvas_height) { |
| 12 this.shape_type = shape_types[Math.floor(Math.random() * 6)]; |
| 13 this.color = get_random_color_string(); |
| 14 |
| 15 var max_size = Math.floor(Math.random() * 160) + 20; |
| 16 |
| 17 var center = new Point(Math.floor(Math.random() * canvas_width), |
| 18 Math.floor(Math.random() * canvas_height)); |
| 19 |
| 20 switch(this.shape_type) { |
| 21 case "quadratic": |
| 22 this.p1 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 23 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 24 |
| 25 this.p2 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 26 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 27 |
| 28 this.p3 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 29 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 30 |
| 31 this.width = Math.floor(Math.random() * 40) + 5; |
| 32 break; |
| 33 |
| 34 case "bezier": |
| 35 this.p1 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 36 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 37 |
| 38 this.p2 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 39 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 40 |
| 41 this.p3 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 42 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 43 |
| 44 this.p4 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 45 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 46 |
| 47 this.width = Math.floor(Math.random() * 40) + 5; |
| 48 break; |
| 49 |
| 50 case "arc_to": |
| 51 this.p1 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 52 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 53 |
| 54 this.p2 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 55 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 56 |
| 57 this.p3 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 58 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 59 |
| 60 this.width = Math.floor(Math.random() * 40) + 5; |
| 61 this.radius = Math.floor(Math.random() * 180) + 20; |
| 62 break; |
| 63 |
| 64 case "arc": |
| 65 this.p1 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 66 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 67 |
| 68 this.start_angle = Math.random() * 2 * Math.PI; |
| 69 this.end_angle = Math.random() * 2 * Math.PI; |
| 70 this.countclockwise = Math.floor(Math.random * 2); |
| 71 |
| 72 this.width = Math.floor(Math.random() * 40) + 5; |
| 73 this.radius = Math.floor(Math.random() * 180) + 20; |
| 74 break; |
| 75 |
| 76 case "rectangle": |
| 77 this.p1 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 78 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 79 |
| 80 this.p2 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 81 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 82 |
| 83 this.p3 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 84 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 85 |
| 86 this.width = Math.floor(Math.random() * 180) + 20; |
| 87 this.height = Math.floor(Math.random() * 180) + 20; |
| 88 this.line_width = Math.floor(Math.random() * 15); |
| 89 break; |
| 90 |
| 91 case "ellipse": |
| 92 this.p1 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 93 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 94 |
| 95 this.radius_w = Math.floor(Math.random() * 180) + 20; |
| 96 this.radius_h = Math.floor(Math.random() * 180) + 20; |
| 97 |
| 98 this.countclockwise = Math.floor(Math.random * 2); |
| 99 this.line_width = Math.floor(Math.random() * 15); |
| 100 |
| 101 this.rotation = Math.random() * 2 * Math.PI; |
| 102 this.start_angle = Math.random() * 2 * Math.PI; |
| 103 this.end_angle = Math.random() * 2 * Math.PI; |
| 104 |
| 105 break; |
| 106 } |
| 107 } |
| 108 |
| 109 this.draw_quadratic_segment = function(context) { |
| 110 context.strokeStyle = this.color; |
| 111 context.lineWidth = this.width; |
| 112 context.beginPath(); |
| 113 context.moveTo(this.p1.x, this.p1.y); |
| 114 context.quadraticCurveTo(this.p2.x, this.p2.y, this.p3.x, this.p3.y); |
| 115 context.stroke(); |
| 116 } |
| 117 |
| 118 this.draw_bezier_segment = function(context) { |
| 119 context.strokeStyle = this.color; |
| 120 context.lineWidth = this.width; |
| 121 context.beginPath(); |
| 122 context.moveTo(this.p1.x, this.p1.y); |
| 123 context.bezierCurveTo(this.p2.x, this.p2.y, this.p3.x, this.p3.y, this.p4.x,
this.p4.y); |
| 124 context.stroke() |
| 125 } |
| 126 |
| 127 this.draw_arc_to_segment = function(context) { |
| 128 context.strokeStyle = this.color; |
| 129 context.lineWidth = this.width; |
| 130 context.beginPath(); |
| 131 context.moveTo(this.p1.x, this.p1.y); |
| 132 context.arcTo(this.p2.x, this.p2.y, this.p3.x, this.p3.y, this.radius); |
| 133 context.stroke(); |
| 134 } |
| 135 |
| 136 this.draw_arc_segment = function(context) { |
| 137 context.strokeStyle = this.color; |
| 138 context.lineWidth = this.width; |
| 139 context.beginPath(); |
| 140 context.arc(this.p1.x, this.p1.y, this.radius, this.start_angle, |
| 141 this.end_angle, this.counterclockwise); |
| 142 context.stroke(); |
| 143 } |
| 144 |
| 145 this.draw_rectangle = function(context) { |
| 146 context.strokeStyle = this.color; |
| 147 context.lineWidth = this.line_width; |
| 148 context.beginPath(); |
| 149 context.rect(this.p1.x, this.p1.y, this.width, this.height); |
| 150 context.stroke(); |
| 151 } |
| 152 |
| 153 this.draw_ellipse = function(context) { |
| 154 context.strokeStyle = this.color; |
| 155 context.lineWidth = this.line_width; |
| 156 context.beginPath(); |
| 157 context.ellipse(this.p1.x, this.p1.y, this.radius_w, this.radius_h, |
| 158 this.rotation, this.start_angle, this.end_angle, this.countercl
ockwise); |
| 159 context.stroke(); |
| 160 } |
| 161 |
| 162 this.draw = function(context) { |
| 163 switch(this.shape_type) { |
| 164 case "quadratic": |
| 165 this.draw_quadratic_segment(context); |
| 166 break; |
| 167 case "bezier": |
| 168 this.draw_bezier_segment(context); |
| 169 break; |
| 170 case "arc_to": |
| 171 this.draw_arc_to_segment(context); |
| 172 break; |
| 173 case "arc": |
| 174 this.draw_arc_segment(context); |
| 175 break; |
| 176 case "rectangle": |
| 177 this.draw_rectangle(context); |
| 178 break; |
| 179 case "ellipse": |
| 180 this.draw_ellipse(context); |
| 181 break; |
| 182 } |
| 183 } |
| 184 } |
| 185 |
| 186 var stage_stroke_shapes = (function() { |
| 187 var stage = {}; |
| 188 var shapes; |
| 189 |
| 190 stage.init = function(number_shapes, canvas_width, canvas_height) { |
| 191 shapes = [] |
| 192 for (var i = 0; i < number_shapes; i++) { |
| 193 var shape = new CanvasStrokeShape(); |
| 194 shape.init(canvas_width, canvas_height); |
| 195 shapes.push(shape); |
| 196 } |
| 197 }; |
| 198 |
| 199 stage.draw = function(context) { |
| 200 for (var i = 0; i < shapes.length; i++) { |
| 201 shapes[i].draw(context); |
| 202 } |
| 203 }; |
| 204 |
| 205 return stage; |
| 206 })(); |
| 207 |
| 208 |
| OLD | NEW |