test/script/jfx/flyingimage.js

Tue, 05 Nov 2013 13:09:40 +0400

author
kshefov
date
Tue, 05 Nov 2013 13:09:40 +0400
changeset 667
bda654c6d59c
parent 638
f22742d5daa3
child 952
6d5471a497fb
permissions
-rw-r--r--

8027708: NASHORN TEST: Create Nashorn test that draws image step-by-step using JavaFX canvas.
Reviewed-by: jlaskey, lagergren

     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  */
    24 /**
    25  * Testing JavaFX canvas run by Nashorn.
    26  *
    27  * @test/nocompare
    28  * @run
    29  * @fork
    30  */
    32 TESTNAME = "flyingimage";
    34 var WIDTH = 800;
    35 var HEIGHT = 600;
    36 var canvas = new Canvas(WIDTH, HEIGHT);
    37 function fileToURL(file) {
    38     return new File(file).toURI().toURL().toExternalForm();
    39 }
    40 var imageUrl = fileToURL(__DIR__ + "flyingimage/flyingimage.png");
    41 var img = new Image(imageUrl);
    42 var isFrameRendered = false;
    43 function renderFrame() {
    44     var t = frame;
    45     var gc = canvas.graphicsContext2D;
    46     gc.setFill(Color.web("#cccccc"));
    47     gc.fillRect(0, 0, WIDTH, HEIGHT);
    48     gc.setStroke(Color.web("#000000"));
    49     gc.setLineWidth(1);
    50     gc.strokeRect(5, 5, WIDTH - 10, HEIGHT - 10);
    51     var c = 200;
    52     var msc= 0.5 * HEIGHT / img.height;
    53     var sp0 = 0.003;
    54     for (var h = 0; h < c; h++) {
    55         gc.setTransform(1, 0, 0, 1, 0, 0);
    56         var yh = h / (c - 1);
    57         gc.translate((0.5 + Math.sin(t * sp0 + h * 0.1) / 3) * WIDTH, 25 + (HEIGHT * 3 / 4 - 40) * (yh * yh));
    58         var sc = 30 / img.height + msc * yh * yh;
    59         gc.rotate(90 * Math.sin(t * sp0 + h * 0.1 + Math.PI));
    60         gc.scale(sc, sc);
    61         gc.drawImage(img, -img.width / 2, -img.height / 2);
    62     }
    63     gc.setTransform(1, 0, 0, 1, 0, 0);
    64     isFrameRendered = true;
    65 }
    66 var stack = new StackPane();
    67 var pane = new BorderPane();
    68 pane.setCenter(canvas);
    69 stack.getChildren().add(pane);
    70 $STAGE.scene = new Scene(stack);
    71 var frame = 0;
    72 var timer = new AnimationTimerExtend() {
    73     handle: function handle(now) {
    74         if (frame < 200) {
    75             renderFrame();
    76             frame++;
    77         } else {
    78             checkImageAndExit();        
    79             timer.stop();
    80         }
    81     }
    82 };
    83 timer.start();

mercurial