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