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