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