nashorn/test/script/jfx/flyingimage.js
author kshefov
Mon, 21 Oct 2013 13:31:03 +0400
changeset 21443 85671274e5fb
child 21690 ffbb4611d1f4
permissions -rw-r--r--
8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas. Reviewed-by: jlaskey, sundar
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 Image                = Java.type("javafx.scene.image.Image");
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    35
var Color                = Java.type("javafx.scene.paint.Color");
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    36
var Canvas               = Java.type("javafx.scene.canvas.Canvas");
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    37
var BorderPane           = Java.type("javafx.scene.layout.BorderPane");
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    38
var StackPane            = Java.type("javafx.scene.layout.StackPane");
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    39
var Font                 = Java.type("javafx.scene.text.Font");
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    40
var FontSmoothingType    = Java.type("javafx.scene.text.FontSmoothingType");
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    41
var Text                 = Java.type("javafx.scene.text.Text");
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    42
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    43
var WIDTH = 800;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    44
var HEIGHT = 600;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    45
var canvas = new Canvas(WIDTH, HEIGHT);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    46
function fileToURL(file) {
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    47
    return new File(file).toURI().toURL().toExternalForm();
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    48
}
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    49
var imageUrl = fileToURL(__DIR__ + "flyingimage/flyingimage.png");
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    50
var img = new Image(imageUrl);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    51
var font = new Font("Arial", 16);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    52
var t = 0;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    53
var isFrameRendered = false;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    54
function renderFrame() {
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    55
    var gc = canvas.graphicsContext2D;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    56
    gc.setFill(Color.web("#cccccc"));
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    57
    gc.fillRect(0, 0, WIDTH, HEIGHT);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    58
    gc.setStroke(Color.web("#000000"));
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    59
    gc.setLineWidth(1);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    60
    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
    61
    var c = 200;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    62
    var msc= 0.5 * HEIGHT / img.height;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    63
    var sp0 = 0.003;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    64
    for (var h = 0; h < c; h++, t++) {
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    65
        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
    66
        var yh = h / (c - 1);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    67
        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
    68
        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
    69
        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
    70
        gc.scale(sc, sc);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    71
        gc.drawImage(img, -img.width / 2, -img.height / 2);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    72
     }
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    73
    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
    74
    isFrameRendered = true;
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    75
}
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    76
var stack = new StackPane();
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    77
var pane = new BorderPane();
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    78
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    79
pane.setCenter(canvas);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    80
stack.getChildren().add(pane);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    81
$STAGE.scene = new Scene(stack);
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    82
renderFrame();
85671274e5fb 8026871: NASHORN TEST: Enable possibility to test Nashorn use of JavaFX canvas.
kshefov
parents:
diff changeset
    83
checkImageAndExit();