javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/HTML5GraphicsEnvironment.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 27 Sep 2013 00:41:07 +0200
branchcanvas
changeset 1306 304e6ee67862
parent 1296 6fed3663b751
child 1452 716529430700
permissions -rw-r--r--
Commenting out currently not needed methods
toni@1111
     1
/**
toni@1111
     2
 * Back 2 Browser Bytecode Translator Copyright (C) 2012 Jaroslav Tulach
toni@1111
     3
 * <jaroslav.tulach@apidesign.org>
toni@1111
     4
 *
toni@1111
     5
 * This program is free software: you can redistribute it and/or modify it under
toni@1111
     6
 * the terms of the GNU General Public License as published by the Free Software
toni@1111
     7
 * Foundation, version 2 of the License.
toni@1111
     8
 *
toni@1111
     9
 * This program is distributed in the hope that it will be useful, but WITHOUT
toni@1111
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
toni@1111
    11
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
toni@1111
    12
 * details.
toni@1111
    13
 *
toni@1111
    14
 * You should have received a copy of the GNU General Public License along with
toni@1111
    15
 * this program. Look for COPYING file in the top folder. If not, see
toni@1111
    16
 * http://opensource.org/licenses/GPL-2.0.
toni@521
    17
 */
toni@1135
    18
package org.apidesign.bck2brwsr.htmlpage;
toni@521
    19
toni@1134
    20
import java.util.HashMap;
toni@1134
    21
import java.util.Set;
toni@1138
    22
import net.java.html.canvas.Dimension;
toni@1145
    23
import net.java.html.canvas.Image;
toni@1145
    24
import net.java.html.canvas.ImageData;
toni@1134
    25
import net.java.html.canvas.Style;
toni@1151
    26
import net.java.html.canvas.Style.Color;
toni@1151
    27
import net.java.html.canvas.Style.LinearGradient;
toni@1151
    28
import net.java.html.canvas.Style.Pattern;
toni@1151
    29
import net.java.html.canvas.Style.RadialGradient;
toni@1138
    30
import net.java.html.canvas.spi.GraphicsEnvironment;
toni@521
    31
import org.apidesign.bck2brwsr.core.JavaScriptBody;
toni@1135
    32
import org.apidesign.bck2brwsr.htmlpage.api.Canvas;
toni@1296
    33
import org.openide.util.lookup.ServiceProvider;
toni@521
    34
toni@521
    35
/**
toni@521
    36
 *
toni@1112
    37
 * @author Anton Epple <toni.epple@eppleton.de>
toni@521
    38
 */
toni@1296
    39
@ServiceProvider(service = GraphicsEnvironment.class)
toni@1134
    40
public class HTML5GraphicsEnvironment implements GraphicsEnvironment {
toni@521
    41
toni@1112
    42
    Object context;
toni@1134
    43
    Canvas canvas;
toni@1142
    44
toni@1296
    45
    public HTML5GraphicsEnvironment() {
toni@1296
    46
    }
toni@1296
    47
toni@1296
    48
    
toni@1296
    49
    
toni@1142
    50
    public HTML5GraphicsEnvironment(Object contextImpl, Canvas canvas) {
toni@1112
    51
        this.context = contextImpl;
toni@1134
    52
        this.canvas = canvas;
toni@1112
    53
    }
toni@1112
    54
toni@1112
    55
    @JavaScriptBody(args = {"centerx", "centery", "radius", "startangle", "endangle", "ccw"},
toni@1112
    56
            body = "this._context().arc(centerx,centery, radius, startangle, endangle,ccw);")
toni@1112
    57
    @Override
toni@1112
    58
    public native void arc(double centerX,
toni@1111
    59
            double centerY,
toni@1111
    60
            double startAngle,
toni@1111
    61
            double radius,
toni@1111
    62
            double endAngle,
toni@1111
    63
            boolean ccw);
toni@521
    64
toni@1112
    65
    @JavaScriptBody(args = {"x1", "y1", "x2", "y2", "r"},
toni@1112
    66
            body = "this._context().arcTo(x1,y1,x2,y2,r);")
toni@1112
    67
    @Override
toni@1112
    68
    public native void arcTo(double x1,
toni@1111
    69
            double y1,
toni@1111
    70
            double x2,
toni@1111
    71
            double y2,
toni@1111
    72
            double r);
toni@521
    73
toni@1112
    74
    @JavaScriptBody(args = {"x", "y"},
toni@1112
    75
            body = "return this._context().isPointInPath(x,y);")
toni@1112
    76
    @Override
toni@1112
    77
    public native boolean isPointInPath(double x, double y);
toni@521
    78
toni@1112
    79
    @JavaScriptBody(args = {}, body = "this._context().fill();")
toni@1112
    80
    @Override
toni@1112
    81
    public native void fill();
toni@521
    82
toni@1112
    83
    @JavaScriptBody(args = {}, body = "this._context().stroke();")
toni@1112
    84
    @Override
toni@1112
    85
    public native void stroke();
toni@1109
    86
toni@1112
    87
    @JavaScriptBody(args = {}, body = "this._context().beginPath();")
toni@1112
    88
    @Override
toni@1112
    89
    public native void beginPath();
toni@1109
    90
toni@1112
    91
    @JavaScriptBody(args = {}, body = "this._context().closePath();")
toni@1112
    92
    @Override
toni@1112
    93
    public native void closePath();
toni@521
    94
toni@1112
    95
    @JavaScriptBody(args = {}, body = "this._context().clip();")
toni@1112
    96
    @Override
toni@1112
    97
    public native void clip();
toni@1109
    98
toni@1112
    99
    @JavaScriptBody(args = {"x", "y"}, body = "this._context().moveTo(x,y);")
toni@1112
   100
    @Override
toni@1112
   101
    public native void moveTo(double x, double y);
toni@1109
   102
toni@1112
   103
    @JavaScriptBody(args = {"x", "y"}, body = "this._context().lineTo(x,y);")
toni@1112
   104
    @Override
toni@1112
   105
    public native void lineTo(double x, double y);
toni@1109
   106
toni@1112
   107
    @JavaScriptBody(args = {"cpx", "cpy", "x", "y"}, body = "this._context().quadraticCurveTo(cpx,cpy,x,y);")
toni@1112
   108
    @Override
toni@1112
   109
    public native void quadraticCurveTo(double cpx, double cpy, double x, double y);
toni@1109
   110
toni@1112
   111
    @JavaScriptBody(args = {"cp1x", "cp1y", "cp2x", "cp2y", "x", "y"},
toni@1112
   112
            body = "this._context().bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y);")
toni@1112
   113
    @Override
toni@1112
   114
    public native void bezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y);
toni@1109
   115
toni@1112
   116
    @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().fillRect(x,y,width,height);")
toni@1112
   117
    @Override
toni@1112
   118
    public native void fillRect(double x, double y, double width, double height);
toni@1109
   119
toni@1112
   120
    @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().strokeRect(x,y,width,height);")
toni@1112
   121
    @Override
toni@1112
   122
    public native void strokeRect(double x, double y, double width, double height);
toni@1109
   123
toni@1112
   124
    @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().clearRect(x,y,width,height);")
toni@1112
   125
    @Override
toni@1112
   126
    public native void clearRect(double x, double y, double width, double height);
toni@1109
   127
toni@1145
   128
    @JavaScriptBody(args = {"x", "y", "width", "height"}, body = "this._context().rect(x,y,width,height);")
toni@1112
   129
    @Override
toni@1112
   130
    public native void rect(double x, double y, double width, double height);
toni@1109
   131
toni@1112
   132
    @JavaScriptBody(args = {}, body = "this._context().save();")
toni@1112
   133
    @Override
toni@1112
   134
    public native void save();
toni@1109
   135
toni@1112
   136
    @JavaScriptBody(args = {}, body = "this._context().restore();")
toni@1112
   137
    @Override
toni@1112
   138
    public native void restore();
toni@1109
   139
toni@1112
   140
    @JavaScriptBody(args = {"angle"}, body = "this._context().rotate(angle);")
toni@1112
   141
    @Override
toni@1112
   142
    public native void rotate(double angle);
toni@1109
   143
toni@1112
   144
    @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().transform(a,b,c,d,e,f);")
toni@1112
   145
    @Override
toni@1112
   146
    public native void transform(double a, double b, double c, double d, double e, double f);
toni@1109
   147
toni@1112
   148
    @JavaScriptBody(args = {"a", "b", "c", "d", "e", "f"}, body = "this._context().setTransform(a,b,c,d,e,f);")
toni@1112
   149
    @Override
toni@1112
   150
    public native void setTransform(double a, double b, double c, double d, double e, double f);
toni@1109
   151
toni@1112
   152
    @JavaScriptBody(args = {"x", "y"}, body = "this._context().translate(x,y);")
toni@1112
   153
    @Override
toni@1112
   154
    public native void translate(double x, double y);
toni@1109
   155
toni@1112
   156
    @JavaScriptBody(args = {"x", "y"}, body = "this._context().scale(x,y);")
toni@1112
   157
    @Override
toni@1112
   158
    public native void scale(double x, double y);
toni@1109
   159
toni@1145
   160
    @Override
toni@1145
   161
    public Object drawImage(Image image, double x, double y, Object nativeImage) {
toni@1145
   162
        if (nativeImage == null) {
toni@1145
   163
            nativeImage = createImage(image.getSrc());
toni@1145
   164
        }
toni@1145
   165
        return drawImageImpl(context, nativeImage, x, y);
toni@1145
   166
    }
toni@1145
   167
toni@1145
   168
    @Override
toni@1145
   169
    public Object drawImage(Image image, double x, double y, double width, double height, Object nativeImage) {
toni@1145
   170
        if (nativeImage == null) {
toni@1145
   171
            nativeImage = createImage(image.getSrc());
toni@1145
   172
        }
toni@1145
   173
        return drawImageImpl(context, nativeImage, x, y, width, height);
toni@1145
   174
    }
toni@1145
   175
toni@1145
   176
    @Override
toni@1145
   177
    public Object drawImage(Image image, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height, Object nativeImage) {
toni@1145
   178
        if (nativeImage == null) {
toni@1145
   179
            nativeImage = createImage(image.getSrc());
toni@1145
   180
        }
toni@1145
   181
        return drawImageImpl(context, nativeImage, sx, sy, sWidth, sHeight, x, y, width, height);
toni@1145
   182
    }
toni@1145
   183
toni@1158
   184
    @JavaScriptBody(args = {"ctx", "img", "x", "y", "width", "height"}, body = "img.onload=function(){ctx.drawImage(img,x,y,width,height);};ctx.drawImage(img,x,y,width,height); return img;")
toni@1145
   185
    private native static Object drawImageImpl(Object ctx, Object img, double x, double y, double width, double height);
toni@1145
   186
toni@1158
   187
    @JavaScriptBody(args = {"ctx", "img", "sx", "sy", "swidth", "sheight", "x", "y", "width", "height"}, body = "img.onload=function(){ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);}; ctx.drawImage(img,sx,sy,swidth,sheight,x,y,width,height); return img;")
toni@1145
   188
    private native static Object drawImageImpl(Object ctx, Object img, double sx, double sy, double sWidth, double sHeight, double x, double y, double width, double height);
toni@1145
   189
toni@1158
   190
    @JavaScriptBody(args = {"ctx", "img", "x", "y"}, body = "img.onload=function(){ctx.drawImage(img,x,y);}; ctx.drawImage(img,x,y); return img;")
toni@1145
   191
    private native static Object drawImageImpl(Object ctx, Object img, double x, double y);
toni@1145
   192
toni@1142
   193
    public Object setFillStyle(Style style, Object nativeStyle) {
toni@1142
   194
        if (nativeStyle == null) {
toni@1142
   195
            nativeStyle = createNativeStyle(style);
toni@1142
   196
        }
toni@1142
   197
        setFillStyleImpl(context, nativeStyle);
toni@1142
   198
        return nativeStyle;
toni@1112
   199
    }
toni@1109
   200
toni@1142
   201
    private Object createNativeStyle(Style style) {
toni@1134
   202
        if (style instanceof RadialGradient) {
toni@1134
   203
            RadialGradientWrapper gradient = createRadialGradientWrapper(
toni@1134
   204
                    ((RadialGradient) style).getX0(),
toni@1134
   205
                    ((RadialGradient) style).getY0(),
toni@1134
   206
                    ((RadialGradient) style).getR0(),
toni@1134
   207
                    ((RadialGradient) style).getX1(),
toni@1134
   208
                    ((RadialGradient) style).getY1(),
toni@1134
   209
                    ((RadialGradient) style).getR1());
toni@1134
   210
            HashMap<Double, String> stops = ((LinearGradient) style).getStops();
toni@1134
   211
            Set<Double> keySet = stops.keySet();
toni@1134
   212
            for (Double double1 : keySet) {
toni@1134
   213
                addColorStopImpl(style, double1, stops.get(double1));
toni@1134
   214
            }
toni@1134
   215
            return gradient;
toni@1134
   216
toni@1134
   217
toni@1134
   218
        } else if (style instanceof LinearGradient) {
toni@1134
   219
            LinearGradientWrapper gradient = createLinearGradientWrapper(
toni@1134
   220
                    ((LinearGradient) style).getX0(),
toni@1134
   221
                    ((LinearGradient) style).getY0(),
toni@1134
   222
                    ((LinearGradient) style).getX1(),
toni@1134
   223
                    ((LinearGradient) style).getY1());
toni@1134
   224
            HashMap<Double, String> stops = ((LinearGradient) style).getStops();
toni@1134
   225
            Set<Double> keySet = stops.keySet();
toni@1134
   226
            for (Double double1 : keySet) {
toni@1134
   227
                addColorStopImpl(style, double1, stops.get(double1));
toni@1134
   228
            }
toni@1134
   229
            return gradient;
toni@1142
   230
        } else if (style instanceof Pattern) {
toni@1149
   231
            return createPatternWrapper(((Pattern) style).getImageResource(), ((Pattern) style).getRepeat());
toni@1149
   232
        } else if (style instanceof Color) {
toni@1149
   233
            return ((Color) style)
toni@1149
   234
                    .getAsString();
toni@1134
   235
        }
toni@1134
   236
        return null;
toni@1112
   237
    }
toni@1109
   238
toni@1134
   239
    @JavaScriptBody(args = {"gradient", "position", "color"}, body =
toni@1134
   240
            "gradient.addColorStop(position,color)")
toni@1134
   241
    private static native void addColorStopImpl(Object gradient, double position, String color);
toni@1109
   242
toni@1296
   243
    @JavaScriptBody(args = {"context", "obj"}, body = "context.fillStyle=obj;")
toni@1112
   244
    private native void setFillStyleImpl(Object context, Object obj);
toni@1109
   245
toni@1112
   246
    @JavaScriptBody(args = {"style"}, body = "this._context().strokeStyle=style.valueOf();")
toni@1112
   247
    public native void setStrokeStyle(String style);
toni@1109
   248
toni@1112
   249
    @Override
toni@1142
   250
    public Object setStrokeStyle(Style style, Object nativeStyle) {
toni@1145
   251
        if (nativeStyle == null) {
toni@1142
   252
            nativeStyle = createNativeStyle(style);
toni@1142
   253
        }
toni@1142
   254
        setStrokeStyleImpl(context, nativeStyle);
toni@1142
   255
        return nativeStyle;
toni@1112
   256
    }
toni@1109
   257
toni@1112
   258
    @JavaScriptBody(args = {"context", "obj"}, body = "context.strokeStyle=obj;")
toni@1112
   259
    private native void setStrokeStyleImpl(Object context, Object obj);
jaroslav@1306
   260
/*
toni@1112
   261
    @JavaScriptBody(args = {"color"}, body = "this._context().shadowColor=color.valueOf();")
toni@1112
   262
    @Override
toni@1112
   263
    public native void setShadowColor(String color);
toni@1109
   264
toni@1112
   265
    @JavaScriptBody(args = {"blur"}, body = "this._context().shadowBlur=blur;")
toni@1112
   266
    @Override
toni@1112
   267
    public native void setShadowBlur(double blur);
toni@1109
   268
toni@1112
   269
    @JavaScriptBody(args = {"x"}, body = "this._context().shadowOffsetX=x;")
toni@1112
   270
    @Override
toni@1112
   271
    public native void setShadowOffsetX(double x);
toni@1109
   272
toni@1112
   273
    @JavaScriptBody(args = {"y"}, body = "this._context().shadowOffsetY=y;")
toni@1112
   274
    @Override
toni@1112
   275
    public native void setShadowOffsetY(double y);
toni@1109
   276
toni@1112
   277
    @JavaScriptBody(args = {}, body = "return this._context().strokeStyle;")
toni@1112
   278
    public native String getStrokeStyle();
toni@1109
   279
toni@1112
   280
    @JavaScriptBody(args = {}, body = "return this._context().shadowColor;")
toni@1112
   281
    @Override
toni@1112
   282
    public native String getShadowColor();
toni@1109
   283
toni@1112
   284
    @JavaScriptBody(args = {}, body = "return this._context().shadowBlur;")
toni@1112
   285
    @Override
toni@1112
   286
    public native double getShadowBlur();
toni@1109
   287
toni@1112
   288
    @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetX;")
toni@1112
   289
    @Override
toni@1112
   290
    public native double getShadowOffsetX();
toni@1109
   291
toni@1112
   292
    @JavaScriptBody(args = {}, body = "return this._context().shadowOffsetY;")
toni@1112
   293
    @Override
toni@1112
   294
    public native double getShadowOffsetY();
jaroslav@1306
   295
*/
toni@1112
   296
    @JavaScriptBody(args = {}, body = "return this._context().lineCap;")
toni@1112
   297
    @Override
toni@1112
   298
    public native String getLineCap();
toni@1109
   299
toni@1112
   300
    @JavaScriptBody(args = {"style"}, body = "this._context().lineCap=style.valueOf();")
toni@1112
   301
    @Override
toni@1112
   302
    public native void setLineCap(String style);
toni@521
   303
toni@1112
   304
    @JavaScriptBody(args = {}, body = "return this._context().lineJoin;")
toni@1112
   305
    @Override
toni@1112
   306
    public native String getLineJoin();
toni@521
   307
toni@1112
   308
    @JavaScriptBody(args = {"style"}, body = "this._context().lineJoin=style.valueOf();")
toni@1112
   309
    @Override
toni@1112
   310
    public native void setLineJoin(String style);
toni@521
   311
toni@1112
   312
    @JavaScriptBody(args = {}, body = "return this._context().lineWidth;")
toni@1112
   313
    @Override
toni@1112
   314
    public native double getLineWidth();
toni@521
   315
toni@1112
   316
    @JavaScriptBody(args = {"width"}, body = "this._context().lineWidth=width;")
toni@1112
   317
    @Override
toni@1112
   318
    public native void setLineWidth(double width);
toni@521
   319
toni@1112
   320
    @JavaScriptBody(args = {}, body = "return this._context().miterLimit;")
toni@1112
   321
    @Override
toni@1112
   322
    public native double getMiterLimit();
toni@521
   323
toni@1112
   324
    @JavaScriptBody(args = {"limit"}, body = "this._context().miterLimit=limit;")
toni@1112
   325
    @Override
toni@1112
   326
    public native void setMiterLimit(double limit);
toni@1111
   327
toni@1112
   328
    @JavaScriptBody(args = {}, body = "return this._context().font;")
toni@1112
   329
    @Override
toni@1112
   330
    public native String getFont();
toni@1111
   331
toni@1112
   332
    @JavaScriptBody(args = {"font"}, body = "this._context().font=font.valueOf();")
toni@1112
   333
    @Override
toni@1112
   334
    public native void setFont(String font);
toni@1111
   335
toni@1112
   336
    @JavaScriptBody(args = {}, body = "return this._context().textAlign;")
toni@1112
   337
    @Override
toni@1112
   338
    public native String getTextAlign();
toni@1111
   339
toni@1112
   340
    @JavaScriptBody(args = {"textalign"}, body = "this._context().textAlign=textalign.valueOf();")
toni@1112
   341
    @Override
toni@1112
   342
    public native void setTextAlign(String textAlign);
toni@1111
   343
toni@1112
   344
    @JavaScriptBody(args = {}, body = "return this._context().textBaseline;")
toni@1112
   345
    @Override
toni@1112
   346
    public native String getTextBaseline();
toni@1111
   347
toni@1112
   348
    @JavaScriptBody(args = {"textbaseline"}, body = "this._context().textBaseline=textbaseline.valueOf();")
toni@1112
   349
    @Override
toni@1112
   350
    public native void setTextBaseline(String textbaseline);
toni@1111
   351
toni@1112
   352
    @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().fillText(text,x,y);")
toni@1138
   353
//    @JavaScriptBody(args = {"text", "x", "y"}, body = "console.log(text);")
toni@1112
   354
    @Override
toni@1112
   355
    public native void fillText(String text, double x, double y);
toni@1111
   356
toni@1112
   357
    @JavaScriptBody(args = {"text", "x", "y", "maxwidth"}, body = "this._context().fillText(text,x,y,maxwidth);")
toni@1112
   358
    @Override
toni@1112
   359
    public void fillText(String text, double x, double y, double maxWidth) {
toni@1112
   360
    }
toni@1111
   361
toni@1112
   362
    @Override
toni@1134
   363
    public Dimension measureText(String text) {
toni@1138
   364
        measureTextImpl(text);
toni@1142
   365
        return new Dimension(1, 1);
toni@1112
   366
    }
toni@1111
   367
toni@1112
   368
    @JavaScriptBody(args = {"text"},
toni@1112
   369
            body = "return this._context().measureText(text);")
toni@1112
   370
    private native Object measureTextImpl(String text);
toni@1111
   371
toni@1112
   372
    @JavaScriptBody(args = {"text", "x", "y"}, body = "this._context().strokeText(text,x,y);")
toni@1112
   373
    @Override
toni@1112
   374
    public native void strokeText(String text, double x, double y);
toni@1111
   375
toni@1112
   376
    @JavaScriptBody(args = {"text", "x", "y", "maxWidth"}, body = "this._context().strokeText(text,x,y,maxWidth);")
toni@1112
   377
    @Override
toni@1112
   378
    public native void strokeText(String text, double x, double y, double maxWidth);
jaroslav@1306
   379
/*
toni@1145
   380
    @Override
toni@1145
   381
    public ImageData createPixelMap(double x, double y) {
toni@1145
   382
        return new ImageDataWrapper(createPixelMapImpl(x, y));
toni@1145
   383
    }
jaroslav@1306
   384
*/
toni@1145
   385
    @JavaScriptBody(args = {"x", "y"},
toni@1145
   386
            body = "return this._context().createImageData(x,y);")
toni@1145
   387
    private native Object createPixelMapImpl(double x, double y);
jaroslav@1306
   388
/*
toni@1145
   389
    @Override
toni@1145
   390
    public ImageData createPixelMap(ImageData imageData) {
toni@1145
   391
        return new ImageDataWrapper(createPixelMapImpl(imageData.getWidth(), imageData.getHeight()));
toni@1145
   392
    }
toni@1145
   393
toni@1145
   394
    @Override
toni@1145
   395
    public ImageData getPixelMap(double x, double y, double width, double height) {
toni@1145
   396
        return new ImageDataWrapper(getPixelMapImpl(x, y, width, height));
toni@1145
   397
    }
jaroslav@1306
   398
*/
toni@1145
   399
    @JavaScriptBody(args = {"x", "y", "width", "height"},
toni@1145
   400
            body = "return this._context().getImageData(x,y,width,height);")
toni@1145
   401
    private native Object getPixelMapImpl(double x, double y, double width, double height);
jaroslav@1306
   402
/*
toni@1145
   403
    @Override
toni@1145
   404
    public void putPixelMap(ImageData imageData, double x, double y) {
toni@1145
   405
        putPixelMapImpl(((ImageDataWrapper) imageData).object(), x, y);
toni@1145
   406
    }
jaroslav@1306
   407
*/
toni@1145
   408
    @JavaScriptBody(args = {"imageData", "x", "y"},
toni@1145
   409
            body = "this._context().putImageData(imageData,x,y);")
toni@1145
   410
    private native void putPixelMapImpl(Object imageData, double x, double y);
jaroslav@1306
   411
/*
toni@1145
   412
    @Override
toni@1145
   413
    public void putPixelMap(ImageData imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight) {
toni@1145
   414
        putPixelMapImpl(((ImageDataWrapper) imageData).object(), x, y, dirtyx, dirtyy, dirtywidth, dirtyheight);
toni@1145
   415
    }
jaroslav@1306
   416
*/
toni@1112
   417
    @JavaScriptBody(args = {"imageData", "x", "y", "dirtyx", "dirtyy", "dirtywidth", "dirtyheight"},
toni@1112
   418
            body = "this._context().putImageData(imageData,x,y, dirtyx, dirtyy, dirtywidth,dirtyheight);")
toni@1145
   419
    private native void putPixelMapImpl(Object imageData, double x, double y, double dirtyx, double dirtyy, double dirtywidth, double dirtyheight);
toni@1112
   420
toni@1112
   421
    @JavaScriptBody(args = {"alpha"}, body = "this._context().globalAlpha=alpha;")
toni@1112
   422
    @Override
toni@1112
   423
    public native void setGlobalAlpha(double alpha);
toni@1112
   424
toni@1112
   425
    @JavaScriptBody(args = {}, body = "return this._context().globalAlpha;")
toni@1112
   426
    @Override
toni@1112
   427
    public native double getGlobalAlpha();
toni@1112
   428
toni@1112
   429
    @JavaScriptBody(args = {"operation"}, body = "this._context().globalCompositeOperation=operation.valueOf();")
toni@1112
   430
    @Override
toni@1112
   431
    public native void setGlobalCompositeOperation(String operation);
toni@1112
   432
toni@1112
   433
    @JavaScriptBody(args = {}, body = "return this._context().globalCompositeOperation;")
toni@1112
   434
    @Override
toni@1112
   435
    public native String getGlobalCompositeOperation();
toni@1112
   436
toni@1134
   437
    public LinearGradientWrapper createLinearGradientWrapper(double x0, double y0, double x1, double y1) {
toni@1134
   438
        return new LinearGradientWrapper(createLinearGradientImpl(context, x0, y0, x1, y1));
toni@1112
   439
    }
toni@1112
   440
toni@1112
   441
    @JavaScriptBody(args = {"context", "x0", "y0", "x1", "y1"}, body = "return context.createLinearGradient(x0,y0,x1,y1);")
toni@1112
   442
    private native Object createLinearGradientImpl(Object context, double x0, double y0, double x1, double y1);
toni@1112
   443
toni@1145
   444
    public PatternWrapper createPatternWrapper(Image image, String repeat) {
toni@1145
   445
        return new PatternWrapper(createPatternImpl(context, image, repeat));
toni@1145
   446
    }
toni@1145
   447
toni@1145
   448
    @JavaScriptBody(args = {"context", "image", "repeat"}, body = "return context.createPattern(image, repeat);")
toni@1145
   449
    private static native Object createPatternImpl(Object context, Image image, String repeat);
toni@1145
   450
toni@1134
   451
    public RadialGradientWrapper createRadialGradientWrapper(double x0, double y0, double r0, double x1, double y1, double r1) {
toni@1134
   452
        return new RadialGradientWrapper(createRadialGradientImpl(context, x0, y0, r0, x1, y1, r1));
toni@1112
   453
    }
toni@1112
   454
toni@1112
   455
    @JavaScriptBody(args = {"context", "x0", "y0", "r0", "x1", "y1", "r1"}, body = "return context.createRadialGradient(x0,y0,r0,x1,y1,r1);")
toni@1112
   456
    private static native Object createRadialGradientImpl(Object context, double x0, double y0, double r0, double x1, double y1, double r1);
toni@1121
   457
toni@1158
   458
//    @JavaScriptBody(args = {"path"}, body = "var b = new Image(); b.src=path; return b;")
toni@1158
   459
//    public native Image getImageForPathImpl(String path);
toni@1121
   460
    @Override
toni@1134
   461
    public int getHeight() {
toni@1142
   462
        return canvas.getHeight();
toni@1121
   463
    }
toni@1134
   464
toni@1134
   465
    @Override
toni@1134
   466
    public int getWidth() {
toni@1142
   467
        return canvas.getWidth();
toni@1134
   468
    }
jaroslav@1306
   469
/*
toni@1134
   470
    @Override
toni@1134
   471
    public void setHeight(int height) {
toni@1142
   472
        canvas.setHeight(height);
toni@1134
   473
    }
toni@1134
   474
toni@1134
   475
    @Override
toni@1134
   476
    public void setWidth(int width) {
toni@1142
   477
        canvas.setWidth(width);
toni@1134
   478
    }
jaroslav@1306
   479
*/
toni@1296
   480
//    @JavaScriptBody(args = {"src"}, body = "var image = new Image();console.log('image complete '+image.complete);image.src = './'+ src; return image;")
toni@1296
   481
    @JavaScriptBody(args = {"src"}, body = "console.log ('looking up image by id '+src);return document.getElementById(src);")
toni@1145
   482
    private static native Object createImage(String src);
toni@1267
   483
toni@1267
   484
    @Override
toni@1267
   485
    public int getWidth(Image image, Object nativeImage) {
toni@1296
   486
       
toni@1267
   487
        if (nativeImage == null) {
toni@1267
   488
            nativeImage = createImage(image.getSrc());
toni@1296
   489
           
toni@1267
   490
        }
toni@1267
   491
        return getWidth(nativeImage);
toni@1267
   492
    }
toni@1267
   493
toni@1267
   494
    @JavaScriptBody(args = {"nativeImage"}, body = "return nativeImage.naturalWidth;")
toni@1267
   495
    private static native int getWidth(Object nativeImage);
toni@1267
   496
toni@1267
   497
    @Override
toni@1267
   498
    public int getHeight(Image image, Object nativeImage) {
toni@1267
   499
        if (nativeImage == null) {
toni@1267
   500
            nativeImage = createImage(image.getSrc());
toni@1267
   501
        }
toni@1267
   502
        return getHeight(nativeImage);
toni@1267
   503
    }
toni@1267
   504
toni@1267
   505
    @JavaScriptBody(args = {"nativeImage"}, body = "return nativeImage.naturalHeight;")
toni@1267
   506
    private static native int getHeight(Object nativeImage);
toni@1267
   507
toni@1267
   508
    @Override
toni@1267
   509
    public Object mergeImages(Image a, Image b, Object cachedA, Object cachedB) {
toni@1267
   510
        return mergeImages(cachedA, cachedB);
toni@1267
   511
    }
toni@1267
   512
toni@1267
   513
    @JavaScriptBody(args = {"img1", "img2"}, body = "var canvas = document.createElement('img');\n"
toni@1267
   514
            + "var context = canvas.getContext(\"2d\");\n"
toni@1267
   515
            + "var width = img1.width;\n"
toni@1267
   516
            + "var height = img1.height;\n"
toni@1267
   517
            + "canvas.width = width;\n"
toni@1267
   518
            + "canvas.height = height;\n"
toni@1267
   519
            + "context.drawImage(img1, 0, 0);\n"
toni@1267
   520
            + "context.drawImage(img2, 0, 0);\n"
toni@1267
   521
            + "url = canvas.toDataURL();\n"
toni@1267
   522
            + "var resultImage = document.createElement('img');\n"
toni@1267
   523
            + "resultImage.src=url;\n"
toni@1267
   524
            + "return resultImage;")
toni@1267
   525
    public static native Object mergeImages(Object img1, Object img2);
toni@521
   526
}