test/script/jfx/kaleidoscope.js

Thu, 12 Oct 2017 19:52:15 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:52:15 +0800
changeset 1205
4112748288bb
parent 962
ac62e33a99b0
parent 952
6d5471a497fb
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /**
aoqi@0 25 * Testing JavaFX canvas run by Nashorn.
aoqi@0 26 *
aoqi@0 27 * @test/nocompare
aoqi@0 28 * @run
aoqi@0 29 * @fork
aoqi@0 30 */
aoqi@0 31
aoqi@0 32 TESTNAME = "kaleidoscope";
aoqi@0 33
aoqi@0 34 var WIDTH = 800;
aoqi@0 35 var HEIGHT = 600;
aoqi@0 36 var canvas = new Canvas(WIDTH, HEIGHT);
aoqi@0 37 var context = canvas.graphicsContext2D;
aoqi@0 38
aoqi@0 39 var x,y;
aoqi@0 40 var p_x,p_y;
aoqi@0 41 var a=0;
aoqi@0 42 var b=0;
aoqi@0 43 var angle=Math.PI/180*8;
aoqi@0 44 var color=0;
aoqi@0 45 var limit1=Math.PI*1.5;
aoqi@0 46 var limit2=Math.PI*1.79;
aoqi@0 47 var c=new Array(6);
aoqi@0 48 var d=new Array(6);
aoqi@0 49 var r,e;
aoqi@0 50 var fade;
aoqi@0 51 var prv_x,prv_y,prv_x2,prv_y2;
aoqi@0 52 var isFrameRendered = false;
aoqi@0 53
aoqi@0 54 function renderFrame() {
attila@962 55 if (!isFrameRendered) {
aoqi@0 56 a=0.2*angle;
attila@962 57 b=0.7*angle;
attila@962 58 r=0;
attila@962 59 fade=32;
attila@962 60 for(var i=0;i<6;i++)
attila@962 61 {
attila@962 62 c[i]=1.0/(i+1)/2;
attila@962 63 d[i]=1.0/(i+1)/2;
attila@962 64 }
attila@962 65 radius=Math.round((WIDTH+HEIGHT)/8);
attila@962 66 e=radius*0.2;
attila@962 67 p_x=Math.round(WIDTH/2);
attila@962 68 p_y=Math.round(HEIGHT/2);
attila@962 69 x=(radius*c[0])*Math.cos(a*d[1])+(radius*c[2])*Math.sin(a*d[3])+(radius*c[4])*Math.sin(a*d[5]);
attila@962 70 y=(radius*c[5])*Math.sin(a*d[4])+(radius*c[3])*Math.cos(a*d[2])+(radius*c[1])*Math.cos(a*d[0]);
aoqi@0 71 isFrameRendered = true;
aoqi@0 72 }
aoqi@0 73 anim();
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 function anim() {
attila@962 77 var a1=Math.cos(a*2);
attila@962 78 var a2=Math.cos(a*4);
attila@962 79 var a3=Math.cos(a);
attila@962 80 var a4=Math.sin(a);
attila@962 81 if(b>limit1&&b<limit2) {
attila@962 82 r+=radius*0.02*a1;
attila@962 83 prv_x=x;
attila@962 84 prv_y=y;
attila@962 85 x=prv_x2+r*a3;
attila@962 86 y=prv_y2+r*a4;
attila@962 87 } else {
attila@962 88 prv_x=x;
attila@962 89 prv_y=y;
attila@962 90 prv_x2=x;
attila@962 91 prv_y2=y;
attila@962 92 x=(radius*c[0])*Math.cos(a*d[1])+(radius*c[2])*Math.sin(a*d[3])+(radius*c[4])*Math.sin(a*d[5]);
attila@962 93 y=(radius*c[5])*Math.sin(a*d[4])+(radius*c[3])*Math.cos(a*d[2])+(radius*c[1])*Math.cos(a*d[0]);
attila@962 94 }
attila@962 95 var c3=16*Math.cos(a*10);
attila@962 96 var c1=Math.floor(56*Math.cos(a*angle*4)+c3);
attila@962 97 var c2=Math.floor(56*Math.sin(a*angle*4)-c3);
attila@962 98 context.lineCap=StrokeLineCap.ROUND;
attila@962 99 context.setStroke(Paint.valueOf('rgba('+(192+c1)+','+(192+c2)+','+(192-c1)+','+(0.01-0.005*-a1)+')'));
attila@962 100 context.lineWidth=e*1.4+e*0.8*a3;
attila@962 101 draw_line(p_x,p_y,prv_x,prv_y,x,y);
attila@962 102 context.lineWidth=e+e*0.8*a3;
attila@962 103 draw_line(p_x,p_y,prv_x,prv_y,x,y);
attila@962 104 context.setStroke(Paint.valueOf('rgba('+(192+c1)+','+(192+c2)+','+(192-c1)+','+(0.06-0.03*-a1)+')'));
attila@962 105 context.lineWidth=e*0.6+e*0.35*a3;
attila@962 106 draw_line(p_x,p_y,prv_x,prv_y,x,y);
attila@962 107 context.setStroke(Paint.valueOf('rgba(0,0,0,0.06)'));
attila@962 108 context.lineWidth=e*0.4+e*0.225*a3;
attila@962 109 draw_line(p_x,p_y,prv_x,prv_y,x,y);
attila@962 110 context.setStroke(Paint.valueOf('rgba('+(192+c1)+','+(192+c2)+','+(192-c1)+','+(0.1-0.075*-a1)+')'));
attila@962 111 context.lineWidth=e*0.2+e*0.1*a3;
attila@962 112 draw_line(p_x,p_y,prv_x,prv_y,x,y);
attila@962 113 context.setStroke(Paint.valueOf('rgba(255,255,255,0.4)'));
attila@962 114 context.lineWidth=e*(0.1-0.05*-a2);
attila@962 115 draw_line(p_x,p_y,prv_x,prv_y,x,y);
attila@962 116 a+=angle*Math.cos(b);
attila@962 117 b+=angle*0.1;
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 function draw_line(x,y,x1,y1,x2,y2) {
attila@962 121 context.beginPath();
attila@962 122 context.moveTo(x+x1,y+y1);
attila@962 123 context.lineTo(x+x2,y+y2);
attila@962 124 context.moveTo(x-x1,y+y1);
attila@962 125 context.lineTo(x-x2,y+y2);
attila@962 126 context.moveTo(x-x1,y-y1);
attila@962 127 context.lineTo(x-x2,y-y2);
attila@962 128 context.moveTo(x+x1,y-y1);
attila@962 129 context.lineTo(x+x2,y-y2);
attila@962 130 context.moveTo(x+y1,y+x1);
attila@962 131 context.lineTo(x+y2,y+x2);
attila@962 132 context.moveTo(x-y1,y+x1);
attila@962 133 context.lineTo(x-y2,y+x2);
attila@962 134 context.moveTo(x-y1,y-x1);
attila@962 135 context.lineTo(x-y2,y-x2);
attila@962 136 context.moveTo(x+y1,y-x1);
attila@962 137 context.lineTo(x+y2,y-x2);
attila@962 138 context.moveTo(x,y+x2);
attila@962 139 context.lineTo(x,y+x1);
attila@962 140 context.moveTo(x,y-x2);
attila@962 141 context.lineTo(x,y-x1);
attila@962 142 context.moveTo(x+x2,y);
attila@962 143 context.lineTo(x+x1,y);
attila@962 144 context.moveTo(x-x2,y);
attila@962 145 context.lineTo(x-x1,y);
attila@962 146 context.stroke();
attila@962 147 context.closePath();
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 var stack = new StackPane();
aoqi@0 151 var pane = new BorderPane();
aoqi@0 152 pane.setCenter(canvas);
aoqi@0 153 stack.getChildren().add(pane);
aoqi@0 154 $STAGE.scene = new Scene(stack);
aoqi@0 155 var frame = 0;
aoqi@0 156 var timer = new AnimationTimerExtend() {
aoqi@0 157 handle: function handle(now) {
aoqi@0 158 if (frame < 800) {
aoqi@0 159 renderFrame();
aoqi@0 160 frame++;
aoqi@0 161 } else {
aoqi@0 162 checkImageAndExit();
aoqi@0 163 timer.stop();
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166 };
aoqi@0 167 timer.start();

mercurial