| 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 fill 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 = ["arc_to", "arc", "rectangle", "ellipse"]; |
| 9 |
| 10 function CanvasFillShape() { |
| 11 this.init = function(canvas_width, canvas_height) { |
| 12 this.shape_type = shape_types[Math.floor(Math.random() * 4)]; |
| 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 "arc_to": |
| 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 this.radius = Math.floor(Math.random() * 180) + 20; |
| 33 break; |
| 34 |
| 35 case "arc": |
| 36 this.p1 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 37 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 38 |
| 39 this.start_angle = Math.random() * 2 * Math.PI; |
| 40 this.end_angle = Math.random() * 2 * Math.PI; |
| 41 this.countclockwise = Math.floor(Math.random * 2); |
| 42 |
| 43 this.width = Math.floor(Math.random() * 40) + 5; |
| 44 this.radius = Math.floor(Math.random() * 180) + 20; |
| 45 break; |
| 46 |
| 47 case "rectangle": |
| 48 this.p1 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 49 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 50 |
| 51 this.p2 = 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.p3 = 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.width = Math.floor(Math.random() * 180) + 20; |
| 58 this.height = Math.floor(Math.random() * 180) + 20; |
| 59 this.line_width = Math.floor(Math.random() * 15); |
| 60 break; |
| 61 |
| 62 case "ellipse": |
| 63 this.p1 = new Point(Math.floor(Math.random() * max_size) - max_size/2 +
center.x, |
| 64 Math.floor(Math.random() * max_size) - max_size/2 +
center.y); |
| 65 |
| 66 this.radius_w = Math.floor(Math.random() * 180) + 20; |
| 67 this.radius_h = Math.floor(Math.random() * 180) + 20; |
| 68 |
| 69 this.countclockwise = Math.floor(Math.random * 2); |
| 70 this.line_width = Math.floor(Math.random() * 15); |
| 71 |
| 72 this.rotation = Math.random() * 2 * Math.PI; |
| 73 this.start_angle = Math.random() * 2 * Math.PI; |
| 74 this.end_angle = Math.random() * 2 * Math.PI; |
| 75 break; |
| 76 } |
| 77 } |
| 78 |
| 79 this.draw_arc_to_segment = function(context) { |
| 80 context.fillStyle = this.color; |
| 81 context.beginPath(); |
| 82 context.moveTo(this.p1.x, this.p1.y); |
| 83 context.arcTo(this.p2.x, this.p2.y, this.p3.x, this.p3.y, this.radius); |
| 84 context.fill(); |
| 85 } |
| 86 |
| 87 this.draw_arc_segment = function(context) { |
| 88 context.fillStyle = this.color; |
| 89 context.beginPath(); |
| 90 context.arc(this.p1.x, this.p1.y, this.radius, this.start_angle, |
| 91 this.end_angle, this.counterclockwise); |
| 92 context.fill(); |
| 93 } |
| 94 |
| 95 this.draw_rectangle_segment = function(context) { |
| 96 context.fillStyle = this.color; |
| 97 context.beginPath(); |
| 98 context.rect(this.p1.x, this.p1.y, this.width, this.height); |
| 99 context.fill(); |
| 100 } |
| 101 |
| 102 this.draw_ellipse_segment = function(context) { |
| 103 context.fillStyle = this.color; |
| 104 context.beginPath(); |
| 105 context.ellipse(this.p1.x, this.p1.y, this.radius_w, this.radius_h, |
| 106 this.rotation, this.start_angle, this.end_angle, |
| 107 this.counterclockwise); |
| 108 context.fill(); |
| 109 } |
| 110 |
| 111 this.draw = function(context) { |
| 112 switch(this.shape_type) { |
| 113 case "arc_to": |
| 114 this.draw_arc_to_segment(context); |
| 115 break; |
| 116 case "arc": |
| 117 this.draw_arc_segment(context); |
| 118 break; |
| 119 case "rectangle": |
| 120 this.draw_rectangle_segment(context); |
| 121 break; |
| 122 case "ellipse": |
| 123 this.draw_ellipse_segment(context); |
| 124 break; |
| 125 } |
| 126 } |
| 127 } |
| 128 |
| 129 var stage_fill_shapes = (function() { |
| 130 var stage = {}; |
| 131 var shapes; |
| 132 |
| 133 stage.init = function(number_shapes, canvas_width, canvas_height) { |
| 134 shapes = []; |
| 135 for (var i = 0; i < number_shapes; i++) { |
| 136 var shape = new CanvasFillShape(); |
| 137 shape.init(canvas_width, canvas_height); |
| 138 shapes.push(shape); |
| 139 } |
| 140 }; |
| 141 |
| 142 stage.draw = function(context) { |
| 143 for (var i = 0; i < shapes.length; i++) { |
| 144 shapes[i].draw(context); |
| 145 } |
| 146 }; |
| 147 |
| 148 return stage; |
| 149 })(); |
| OLD | NEW |