nashorn/test/script/jfx/spread.js
author rprotacio
Tue, 06 Sep 2016 22:52:35 +0200
changeset 40921 cc129ac8e609
parent 24778 2ff5d7041566
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21690
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
     1
/*
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
     4
 * 
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
     8
 * 
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    13
 * accompanied this code).
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    14
 * 
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    18
 * 
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    21
 * questions.
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    22
 */
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    23
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    24
/**
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    25
 * Testing JavaFX canvas run by Nashorn.
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    26
 *
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    27
 * @test/nocompare
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    28
 * @run
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    29
 * @fork
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    30
 */
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    31
 
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    32
TESTNAME = "spread";
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    33
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    34
var WIDTH = 800;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    35
var HEIGHT = 600;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    36
var canvas = new Canvas(WIDTH, HEIGHT);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    37
var context = canvas.graphicsContext2D;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    38
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    39
/* "Spread" tech demo of canvas by Tom Theisen
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    40
 *
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    41
 * This will animate a sequence of branch structures in a canvas element.
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    42
 * Each frame, a new direction is calculated, similar to the last frame.
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    43
 */
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    44
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    45
var start_width = 20;           // starting width of each branch
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    46
var frame_time = 30;            // milliseconds per frame
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    47
var straighten_factor = 0.95;   // value from 0 to 1, factor applied to direction_offset every frame
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    48
var curviness = 0.2;            // amount of random direction change each frame
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    49
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    50
var color_speed = 0.03;     // speed at which colors change when cycling is enabled
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    51
var branch_shrink = 0.95;   // factor by which branches shrink every frame
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    52
var min_width = 1;          // minimum WIDTH for branch, after which they are discontinued
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    53
var branch_opacity = 0.4;   // opacity of lines drawn
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    54
var branch_count = 3;       // branch count per tree
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    55
var branch_bud_size = 0.5;  // ratio of original branch size at which branch will split
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    56
var branch_bud_angle = 1;   // angle offset for split branch;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    57
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    58
var paper;                  // reference to graphics context
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    59
var branches = Object();    // linked list of active branches
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    60
var color_styles = [];      // pre-computed list of colors as styles. format: (r,g,b,a)    
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    61
var direction_offset = 0;   // current direction offset in radians.  this is applied to all branches.
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    62
var frame = 0;              // frame counter
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    63
var timespent = 0;          // total time spent so far, used to calculate average frame render duration
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    64
var frameratespan;          // html span element for updating performance number
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    65
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    66
// preferences object, contains an attribute for each user setting
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    67
var prefs = {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    68
    wrap: true,             // causes branches reaching edge of viewable area to appear on opposite side
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    69
    fade: false,             // fade existing graphics on each frame
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    70
    cycle: true,            // gradually change colors each frame
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    71
    new_branch_frames: 20    // number of frames elapsed between each auto-generated tree
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    72
};
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    73
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    74
// create tree at the specified position with number of branches
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    75
function create_tree(branches, start_width, position, branch_count) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    76
    var angle_offset = Math.PI * 2 / branch_count;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    77
    for (var i = 0; i < branch_count; ++i) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    78
        branch_add(branches, new Branch(position, angle_offset * i, start_width));
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    79
    }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    80
}
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    81
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    82
// add branch to collection
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    83
function branch_add(branches, branch) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    84
    branch.next = branches.next;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    85
    branches.next = branch;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    86
}
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    87
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    88
// get the coordinates for the position of a new tree
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    89
// use the center of the canvas
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    90
function get_new_tree_center(width, height) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    91
    return {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    92
        x: 0.5 * width, 
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    93
        y: 0.5 * height 
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    94
    };
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    95
}
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    96
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    97
// Branch constructor
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    98
// position has x and y properties
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
    99
// direction is in radians
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   100
function Branch(position, direction, width) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   101
    this.x = position.x;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   102
    this.y = position.y;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   103
    this.width = width;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   104
    this.original_width = width;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   105
    this.direction = direction;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   106
}
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   107
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   108
// update position, direction and width of a particular branch
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   109
function branch_update(branches, branch, paper) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   110
    paper.beginPath();
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   111
    paper.lineWidth = branch.width;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   112
    paper.moveTo(branch.x, branch.y);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   113
    
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   114
    branch.width *= branch_shrink;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   115
    branch.direction += direction_offset;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   116
    branch.x += Math.cos(branch.direction) * branch.width;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   117
    branch.y += Math.sin(branch.direction) * branch.width;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   118
    
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   119
    paper.lineTo(branch.x, branch.y);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   120
    paper.stroke();
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   121
    
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   122
    if (prefs.wrap) wrap_branch(branch, WIDTH, HEIGHT);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   123
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   124
    if (branch.width < branch.original_width * branch_bud_size) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   125
        branch.original_width *= branch_bud_size;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   126
        branch_add(branches, new Branch(branch, branch.direction + 1, branch.original_width));
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   127
    }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   128
}
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   129
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   130
function draw_frame() {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   131
    if (prefs.fade) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   132
        paper.fillRect(0, 0, WIDTH, HEIGHT);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   133
    }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   134
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   135
    if (prefs.cycle) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   136
        paper.setStroke(Paint.valueOf(color_styles[frame % color_styles.length]));
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   137
    }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   138
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   139
    if (frame++ % prefs.new_branch_frames == 0) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   140
        create_tree(branches, start_width, get_new_tree_center(WIDTH, HEIGHT), branch_count);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   141
    }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   142
    
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   143
    direction_offset += (0.35 + (frame % 200) * 0.0015) * curviness - curviness / 2;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   144
    direction_offset *= straighten_factor;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   145
    
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   146
    var branch = branches;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   147
    var prev_branch = branches;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   148
    while (branch = branch.next) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   149
        branch_update(branches, branch, paper);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   150
        
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   151
        if (branch.width < min_width) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   152
            // remove branch from list
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   153
            prev_branch.next = branch.next;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   154
        }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   155
        
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   156
        prev_branch = branch;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   157
    }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   158
}
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   159
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   160
// constrain branch position to visible area by "wrapping" from edge to edge
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   161
function wrap_branch(branch, WIDTH, HEIGHT) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   162
    branch.x = positive_mod(branch.x, WIDTH);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   163
    branch.y = positive_mod(branch.y, HEIGHT);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   164
}
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   165
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   166
// for a < 0, b > 0, javascript returns a negative number for a % b
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   167
// this is a variant of the % operator that adds b to the result in this case
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   168
function positive_mod(a, b) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   169
    // ECMA 262 11.5.3: Applying the % Operator 
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   170
    // remainder operator does not convert operands to integers,
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   171
    // although negative results are possible
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   172
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   173
    return ((a % b) + b) % b;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   174
}
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   175
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   176
// pre-compute color styles that will be used for color cycling
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   177
function populate_colors(color_speed, color_styles, branch_opacity) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   178
    // used in calculation of RGB values
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   179
    var two_thirds_pi = Math.PI * 2 / 3;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   180
    var four_thirds_pi = Math.PI * 4 / 3;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   181
    var two_pi = Math.PI * 2;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   182
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   183
    // hue does represent hue, but not in the conventional HSL scheme
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   184
    for(var hue = 0; hue < two_pi; hue += color_speed) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   185
        var r = Math.floor(Math.sin(hue) * 128 + 128);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   186
        var g = Math.floor(Math.sin(hue + two_thirds_pi) * 128 + 128);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   187
        var b = Math.floor(Math.sin(hue + four_thirds_pi) * 128 + 128);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   188
        color = "rgba(" + [r, g, b, branch_opacity].join() + ")";
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   189
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   190
        color_styles.push(color);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   191
    }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   192
}
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   193
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   194
// apply initial settings to canvas object
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   195
function setup_canvas() {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   196
    paper = canvas.graphicsContext2D;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   197
    paper.setFill(Paint.valueOf('rgb(0, 0, 0)'));
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   198
    paper.fillRect(0, 0, WIDTH, HEIGHT);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   199
    paper.setFill(Paint.valueOf("rgba(0, 0, 0, 0.005)"));
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   200
    paper.setStroke(Paint.valueOf("rgba(128, 128, 64, " + String(branch_opacity) + ")"));
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   201
}
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   202
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   203
populate_colors(color_speed, color_styles, branch_opacity);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   204
setup_canvas();
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   205
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   206
var stack = new StackPane();
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   207
var pane = new BorderPane();
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   208
pane.setCenter(canvas);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   209
stack.getChildren().add(pane);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   210
$STAGE.scene = new Scene(stack);
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   211
var timer = new AnimationTimerExtend() {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   212
    handle: function handle(now) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   213
        if (frame < 200) {
24778
2ff5d7041566 8044638: Tidy up Nashorn codebase for code standards
attila
parents: 21690
diff changeset
   214
            draw_frame();
21690
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   215
        } else {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   216
            checkImageAndExit();
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   217
            timer.stop();
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   218
        }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   219
    }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   220
};
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   221
timer.start();
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents:
diff changeset
   222