nashorn/test/script/jfx/flyingimage.js
author rprotacio
Tue, 06 Sep 2016 22:52:35 +0200
changeset 40921 cc129ac8e609
parent 21690 ffbb4611d1f4
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21443
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
     1
/*
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
     4
 * 
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
     8
 * 
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    13
 * accompanied this code).
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    14
 * 
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    18
 * 
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    21
 * questions.
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    22
 */
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    23
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    24
/**
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    25
 * Testing JavaFX canvas run by Nashorn.
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    26
 *
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    27
 * @test/nocompare
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    28
 * @run
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    29
 * @fork
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    30
 */
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    31
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    32
TESTNAME = "flyingimage";
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    33
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    34
var WIDTH = 800;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    35
var HEIGHT = 600;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    36
var canvas = new Canvas(WIDTH, HEIGHT);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    37
function fileToURL(file) {
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    38
    return new File(file).toURI().toURL().toExternalForm();
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    39
}
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    40
var imageUrl = fileToURL(__DIR__ + "flyingimage/flyingimage.png");
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    41
var img = new Image(imageUrl);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    42
var isFrameRendered = false;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    43
function renderFrame() {
21690
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    44
    var t = frame;
21443
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    45
    var gc = canvas.graphicsContext2D;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    46
    gc.setFill(Color.web("#cccccc"));
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    47
    gc.fillRect(0, 0, WIDTH, HEIGHT);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    48
    gc.setStroke(Color.web("#000000"));
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    49
    gc.setLineWidth(1);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    50
    gc.strokeRect(5, 5, WIDTH - 10, HEIGHT - 10);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    51
    var c = 200;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    52
    var msc= 0.5 * HEIGHT / img.height;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    53
    var sp0 = 0.003;
21690
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    54
    for (var h = 0; h < c; h++) {
21443
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    55
        gc.setTransform(1, 0, 0, 1, 0, 0);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    56
        var yh = h / (c - 1);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    57
        gc.translate((0.5 + Math.sin(t * sp0 + h * 0.1) / 3) * WIDTH, 25 + (HEIGHT * 3 / 4 - 40) * (yh * yh));
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    58
        var sc = 30 / img.height + msc * yh * yh;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    59
        gc.rotate(90 * Math.sin(t * sp0 + h * 0.1 + Math.PI));
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    60
        gc.scale(sc, sc);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    61
        gc.drawImage(img, -img.width / 2, -img.height / 2);
21690
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    62
    }
21443
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    63
    gc.setTransform(1, 0, 0, 1, 0, 0);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    64
    isFrameRendered = true;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    65
}
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    66
var stack = new StackPane();
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    67
var pane = new BorderPane();
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    68
pane.setCenter(canvas);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    69
stack.getChildren().add(pane);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    70
$STAGE.scene = new Scene(stack);
21690
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    71
var frame = 0;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    72
var timer = new AnimationTimerExtend() {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    73
    handle: function handle(now) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    74
        if (frame < 200) {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    75
            renderFrame();
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    76
            frame++;
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    77
        } else {
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    78
            checkImageAndExit();        
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    79
            timer.stop();
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    80
        }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    81
    }
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    82
};
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    83
timer.start();
ffbb4611d1f4 8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
kshefov
parents: 21443
diff changeset
    84