com.google.gwt.canvas.dom.client.Context2d Java Examples

The following examples show how to use com.google.gwt.canvas.dom.client.Context2d. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DataStore.java    From djvu-html5 with GNU General Public License v2.0 6 votes vote down vote up
private CanvasElement prepareMissingTileImage() {
	int tileSize = DjvuContext.getTileSize();
	CanvasElement canvas = createImage(tileSize, tileSize);
	Context2d context2d = canvas.getContext2d();
	context2d.setFillStyle("white");
	context2d.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
	Image image = new Image();
	final ImageElement imageElement = image.getElement().cast();
	imageElement.getStyle().setProperty("visibility", "hidden");
	Event.setEventListener(imageElement, event -> {
		if (Event.ONLOAD == event.getTypeInt()) {
			missingTileImage.getContext2d().drawImage(imageElement, 0, 0);
			RootPanel.get().getElement().removeChild(imageElement);
		}
	});
	RootPanel.get().getElement().appendChild(imageElement);
	image.setUrl(getBlankImageUrl());
	return canvas;
}
 
Example #2
Source File: DataStore.java    From djvu-html5 with GNU General Public License v2.0 6 votes vote down vote up
public void setTile(TileInfo tileInfo, GMap bufferGMap) {
	if (bufferImageData == null || bufferImageData.getWidth() != bufferGMap.getDataWidth()
			|| bufferImageData.getHeight() != bufferGMap.getDataHeight()) {
		bufferImageData = bufferCanvas.getContext2d()
				.createImageData(bufferGMap.getDataWidth(), bufferGMap.getDataHeight());
	}
	Uint8Array imageArray = bufferImageData.getData().cast();
	imageArray.set(bufferGMap.getImageData());
	bufferCanvas.getContext2d().putImageData(bufferImageData, -bufferGMap.getBorder(), 0);

	CanvasElement tile = tiles.get(tileInfo);
	if (tile == null) {
		tile = createImage(bufferGMap.getDataWidth() - bufferGMap.getBorder(), bufferGMap.getDataHeight());
		tiles.put(new TileInfo(tileInfo), tile);
	}
	Context2d c = tile.getContext2d();
	c.setFillStyle("white");
	c.fillRect(0, 0, tileSize, tileSize);
	c.drawImage(bufferCanvas, 0, 0);
	for (Consumer<Integer> listener : tileListeners)
		listener.accept(tileInfo.page);
}
 
Example #3
Source File: TexturedCube.java    From TGAReader with MIT License 6 votes vote down vote up
Canvas createImageCanvas(int [] pixels, int width, int height) {

	    Canvas canvas = Canvas.createIfSupported();
	    canvas.setCoordinateSpaceWidth(width);
	    canvas.setCoordinateSpaceHeight(height);

	    Context2d context = canvas.getContext2d();
	    ImageData data = context.createImageData(width, height);

	    CanvasPixelArray array = data.getData();
	    for(int i=0; i<width*height; i++) {
	        array.set(4*i+0, pixels[i] & 0xFF);
	        array.set(4*i+1, (pixels[i] >> 8) & 0xFF);
	        array.set(4*i+2, (pixels[i] >> 16) & 0xFF);
	        array.set(4*i+3, (pixels[i] >> 24) & 0xFF);
	    }
	    context.putImageData(data, 0, 0);

	    return canvas;

	}
 
Example #4
Source File: ImageCanvasTest.java    From TGAReader with MIT License 6 votes vote down vote up
private Canvas createImageCanvas(int [] pixels, int width, int height) {
	
	Canvas canvas = Canvas.createIfSupported();
	canvas.setCoordinateSpaceWidth(width);
	canvas.setCoordinateSpaceHeight(height);
	
	Context2d context = canvas.getContext2d();
	ImageData data = context.createImageData(width, height);

	CanvasPixelArray array = data.getData();
	for(int i=0; i<width*height; i++) { // ABGR
		array.set(4*i+0, pixels[i] & 0xFF);
		array.set(4*i+1, (pixels[i] >> 8) & 0xFF);
		array.set(4*i+2, (pixels[i] >> 16) & 0xFF);
		array.set(4*i+3, (pixels[i] >> 24) & 0xFF);
	}
	context.putImageData(data, 0, 0);
	
	canvas.getElement().getStyle().setMargin(4, Unit.PX);
	
	return canvas;
	
}
 
Example #5
Source File: GWTDepictor.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void drawString(String theString, double x, double y) {
    ctx.setFont(currentFont);
    ctx.setTextAlign(Context2d.TextAlign.CENTER);
    ctx.setTextBaseline(Context2d.TextBaseline.MIDDLE);
    ctx.setFillStyle(currentColor);
    ctx.fillText(theString, x, y);
}
 
Example #6
Source File: TextMetricsCalculator.java    From jetpad-projectional-open-source with Apache License 2.0 5 votes vote down vote up
private static int measureHeight(Font font, String text) {
  Canvas canvas = canvas();
  Context2d ctx = canvas.getContext2d();

  ctx.setFont(getFontString(font));
  ctx.setFillStyle("rgb(255, 0, 0)");

  int width = (int) ctx.measureText(text).getWidth();
  int canvasHeight = font.getSize() * 2;
  canvas.setHeight(canvasHeight + "px");
  canvas.setHeight(font.getSize() * 2 + "px");
  canvas.setWidth(width + "px");

  ctx.fillText(text, 0, font.getSize());
  ImageData data = ctx.getImageData(0, 0, width, canvasHeight);
  int firstY = canvasHeight - 1;
  int lastY = 0;
  for (int x = 0; x < width; x++) {
    for (int y = 0; y < canvasHeight; y++) {
      int red = data.getRedAt(x, y);
      if (red != 0) {
        if (firstY > y) {
          firstY = y;
        }
        if (lastY < y) {
          lastY = y;
        }
      }
    }
  }
  return lastY - firstY;
}
 
Example #7
Source File: SVGToolBarImpl.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Drawing function
 *
 * @param canvas
 */
private void draw(Canvas canvas) {
  Context2d context2d = canvas.getContext2d();
  GraphicsContext ctx = new GraphicsContext(context2d);
  drawButtons(ctx);
  drawESRButtons(ctx);
  drawHover(ctx);
}
 
Example #8
Source File: Util.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void scaleCanvas(Canvas canvas, int width, int height) {
  canvas.setWidth(width + "px");
  canvas.setHeight(height + "px");

  Context2d ctx = canvas.getContext2d();

  double ratio = Util.getDevicePixelRatio();
  canvas.setCoordinateSpaceWidth((int)(width * ratio));
  canvas.setCoordinateSpaceHeight((int)(height * ratio));
  ctx.scale(ratio, ratio);
}
 
Example #9
Source File: MoleculeDrawDepictor.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void paintDrawingObjects(Context2d g) {
  if (mDrawingObjectList != null) {
    for (int i = 0; i < mDrawingObjectList.size(); i++) {
      IDrawingObject object = mDrawingObjectList.get(i);
      object.draw(new com.actelion.research.gwt.gui.viewer.GraphicsContext(ctx), mTransformation);
    }
  }
}
 
Example #10
Source File: MoleculeDrawDepictor.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void paintStructures(Context2d g) {
  if (mDepictor != null) {
    for (int i = 0; i < mDepictor.length; i++) {
      mDepictor[i].setDisplayMode(mDisplayMode);
      mDepictor[i].paint(g);
    }
  }
}
 
Example #11
Source File: MoleculeDrawDepictor.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void paintFragmentNumbers(Context2d g) {
  if (mFragmentNoColor != null && mMolecule != null) {
    double averageBondLength = calculateAverageBondLength();
    g.save();
    g.setStrokeStyle(mFragmentNoColor);
    g.setFillStyle(mFragmentNoColor);
    int size = (int) (1.6 * averageBondLength);
    String font = "bold " + size + "px Helvetica";
    g.setFont(font);
    for (int i = 0; i < mMolecule.length; i++) {
      if (mMolecule[i].getAllAtoms() != 0) {
        Point cog = new Point();
        for (int atom = 0; atom < mMolecule[i].getAllAtoms(); atom++) {
          cog.x += mMolecule[i].getAtomX(atom);
          cog.y += mMolecule[i].getAtomY(atom);
        }
        cog.x /= mMolecule[i].getAllAtoms();
        cog.y /= mMolecule[i].getAllAtoms();
        cog.x = (int) mDepictor[i].getTransformation().transformX(cog.x);
        cog.y = (int) mDepictor[i].getTransformation().transformY(cog.y);

        String str = (mReactantOrCoreCount == -1) ? "F" + (i + 1)
            : (i < mReactantOrCoreCount) ? "" + (char) ('A' + i)
                : (mIsMarkushStructure) ? "R" + (i + 1 - mReactantOrCoreCount) : "P" + (i + 1 - mReactantOrCoreCount);
        drawString(g, str, cog.x, cog.y);
      }
    }
    g.restore();
  }
}
 
Example #12
Source File: DrawArea.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private AbstractExtendedDepictor createDepictor() {
  AbstractExtendedDepictor mDepictor;
  Context2d ctx = canvas.getContext2d();
  if (isReaction())
    mDepictor = new MoleculeDrawDepictor(ctx, new Reaction(model.getFragments(), model.getReactantCount()),
        model.getDrawingObjects(), false, builder.getDrawConfig());
  else if (isMarkush()) {
    mDepictor = new MoleculeDrawDepictor(ctx, model.getFragments(), model.getMarkushCount(), null,
        builder.getDrawConfig());
  } else {
    mDepictor = new MoleculeDrawDepictor(ctx, model.getMolecule(), model.getDrawingObjects(),
        builder.getDrawConfig());
  }
  return mDepictor;
}
 
Example #13
Source File: DrawArea.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void draw(Canvas canvas) {
  long fg = builder.getForegroundColor();
  Context2d context2d = canvas.getContext2d();
  int w = (int) model.getDisplaySize().getWidth();
  int h = (int) model.getDisplaySize().getHeight();

  int displayMode = model.getDisplayMode();

  drawBackground(context2d, w, h);

  if (isReaction()) {
    model.analyzeFragmentMembership();
    if (model.needsLayout())
      model.cleanReaction(true);
  }

  AbstractExtendedDepictor depictor = createDepictor();
  depictor.setDisplayMode(displayMode);

  if (model.needsLayout()) {
    depictor.updateCoords(null, new java.awt.geom.Rectangle2D.Double(0, 0, (float) w, (float) h),
        AbstractDepictor.cModeInflateToMaxAVBL);
  }
  model.needsLayout(false);

  depictor.paint(context2d);
  // Let the actions draw if needed e.g. NewChainAction
  if (action != null) {
    GraphicsContext ctx = new GraphicsContext(context2d);
    ctx.save();
    ctx.setStroke(fg);
    ctx.setFill(fg);
    action.paint(ctx);
    ctx.restore();
  }
}
 
Example #14
Source File: GWTDepictor.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
protected void drawDottedLine(DepictorLine theLine) {
    ctx.beginPath();
    ctx.setStrokeStyle(currentColor);
    ctx.setLineCap(Context2d.LineCap.ROUND);
    ctx.moveTo(theLine.x1, theLine.y1);
    ctx.lineTo(theLine.x2, theLine.y2);
    ctx.stroke();
}
 
Example #15
Source File: GWTDepictor.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void paint(Object g) {
    if (g instanceof Context2d) {
        ctx = (Context2d) g;
    }
    super.paint(g);
}
 
Example #16
Source File: StructureView.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void drawIDCode(CanvasElement el, String idcode, String coordinates, int displayMode,
        String[] atomText) {
    Canvas canvas = Canvas.wrap(el);
    if (idcode != null && idcode.length() > 0) {
        String combined = idcode + (coordinates != null ? " " + coordinates : "");
        Context2d ctx = canvas.getContext2d();
        drawMolecule(ctx, combined, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight(),
                displayMode, atomText);
    }
}
 
Example #17
Source File: GraphicsContext.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void drawLine(double x, double y, double x1, double y1) {
    ctx.setLineCap(Context2d.LineCap.ROUND);
    ctx.setLineJoin(Context2d.LineJoin.MITER);
    ctx.beginPath();
    ctx.moveTo(x, y);
    ctx.lineTo(x1, y1);
    ctx.stroke();
}
 
Example #18
Source File: MaterialCameraCapture.java    From gwt-material-addins with Apache License 2.0 5 votes vote down vote up
/**
 * Native call to capture the frame of the video stream.
 */
protected String nativeCaptureToDataURL(CanvasElement canvas, Element element, String mimeType) {
    VideoElement videoElement = (VideoElement) element;
    int width = videoElement.getVideoWidth();
    int height = videoElement.getVideoHeight();
    if (Double.isNaN(width) || Double.isNaN(height)) {
        width = videoElement.getClientWidth();
        height = videoElement.getClientHeight();
    }
    canvas.setWidth(width);
    canvas.setHeight(height);
    Context2d context = canvas.getContext2d();
    context.drawImage(videoElement, 0, 0, width, height);
    return canvas.toDataUrl(mimeType);
}
 
Example #19
Source File: SinglePageLayout.java    From djvu-html5 with GNU General Public License v2.0 5 votes vote down vote up
public void redraw() {
	Context2d graphics2d = canvas.getContext2d();
	int w = canvas.getCoordinateSpaceWidth(), h = canvas.getCoordinateSpaceHeight();
	graphics2d.setFillStyle(background);
	graphics2d.fillRect(0, 0, w, h);
	if (pageInfo == null)
		return;

	int subsample = toSubsample(zoom);
	double scale = zoom / toZoom(subsample);
	graphics2d.save();
	int startX = w / 2 - centerX, startY = h / 2 - centerY;
	graphics2d.translate(startX, startY);
	graphics2d.scale(scale, scale);
	graphics2d.translate(-startX, -startY);
	graphics2d.scale(1, -1); // DjVu images have y-axis inverted 

	int tileSize = DjvuContext.getTileSize();
	int pw = (int) (pageInfo.width * zoom), ph = (int) (pageInfo.height * zoom);
	range.xmin = (int) (Math.max(0, centerX - w * 0.5) / tileSize / scale);
	range.xmax = (int) Math.ceil(Math.min(pw, centerX + w * 0.5) / tileSize / scale);
	range.ymin = (int) (Math.max(0, centerY - h * 0.5) / tileSize / scale);
	range.ymax = (int) Math.ceil(Math.min(ph, centerY + h * 0.5) / tileSize / scale);
	imagesArray = dataStore.getTileImages(page, subsample, range , imagesArray);
	for (int y = range.ymin; y <= range.ymax; y++) {
		for (int x = range.xmin; x <= range.xmax; x++) {
			CanvasElement canvasElement = imagesArray[y - range.ymin][x - range.xmin];
			for (int repeats = scale == 1 ? 1 : 3; repeats > 0; repeats--) {
				graphics2d.drawImage(canvasElement, startX + x * tileSize,
						-startY - y * tileSize - canvasElement.getHeight());
			}
		}
	}
	graphics2d.restore();
	// missing tile graphics may exceed the page boundary
	graphics2d.fillRect(startX + pw, 0, w, h);
	graphics2d.fillRect(0, startY + ph, w, h);

	DjvuContext.setTileRange(range, subsample);
}
 
Example #20
Source File: StructureView.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void draw() {
    Element el = canvas.getCanvasElement();
    String idcode = el.getAttribute("data-idcode");
    String text = el.getAttribute("data-text");
    String[] at = text != null ? text.split(",") : null;
    Context2d ctx = canvas.getContext2d();
    drawMolecule(ctx, idcode, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight(), 0, at);
}
 
Example #21
Source File: StructureView.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void draw(CanvasElement el, String idcode, String coordinates) {
    Canvas canvas = Canvas.wrap(el);
    if (idcode != null && idcode.length() > 0) {
        String combined = idcode + (coordinates != null ? " " + coordinates : "");
        Context2d ctx = canvas.getContext2d();
        drawMolecule(ctx, combined, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight());
    }
}
 
Example #22
Source File: StructureView.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void drawMolecule(Context2d ctx, StereoMolecule mol, int width, int height, int displayMode,
        String[] atomText) {
    AbstractDepictor depictor = new GWTDepictor(mol, displayMode);
    if (atomText != null)
        depictor.setAtomText(atomText);
    depictor.validateView(null, new Rectangle2D.Double(0, 0, (float) width, (float) height),
            AbstractDepictor.cModeInflateToMaxAVBL);
    ctx.clearRect(0, 0, width, height);
    depictor.paint(ctx);
}
 
Example #23
Source File: StructureView.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static void drawMolecule(Context2d ctx, String idcode, int width, int height, int displayMode,
        String[] atomText) {
    if (idcode != null && idcode.trim().length() > 0) {
        StereoMolecule mol = new StereoMolecule();
        IDCodeParser parser = new IDCodeParser();
        String[] elements = idcode.split(" ");
        if (elements == null || elements.length == 1)
            parser.parse(mol, idcode);
        else
            parser.parse(mol, elements[0], elements[1]);
        drawMolecule(ctx, mol, width, height, displayMode, atomText);
    } else {
        ctx.clearRect(0, 0, width, height);
    }
}
 
Example #24
Source File: GraphicsContext.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Context2d getContext() {
    return ctx;
}
 
Example #25
Source File: StructureView.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static void drawMolecule(CanvasElement el, JSMolecule mol, int displayMode, String[] atomText) {
    Canvas canvas = Canvas.wrap(el);
    Context2d ctx = canvas.getContext2d();
    drawMolecule(ctx, mol.getStereoMolecule(), canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight(),
            displayMode, atomText);
}
 
Example #26
Source File: TextMetricsCalculator.java    From jetpad-projectional-open-source with Apache License 2.0 4 votes vote down vote up
static double calculateWidth(Font font, String text) {
  Canvas canvas = canvas();
  Context2d ctx = canvas.getContext2d();
  ctx.setFont(getFontString(font));
  return ctx.measureText(normalize(text)).getWidth();
}
 
Example #27
Source File: ToolBarImpl.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void draw(Canvas toolBar) {
  Context2d context2d = toolBar.getContext2d();
  GraphicsContext ctx = new GraphicsContext(context2d);
  drawButtons(ctx);
  drawESRButtons(ctx);
}
 
Example #28
Source File: StructureEditor.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Context2d getContext2d() {
  return drawPane.canvas.getContext2d();
}
 
Example #29
Source File: GraphicsContext.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public GraphicsContext(Context2d ctx) {
    this.ctx = ctx;
}
 
Example #30
Source File: AbstractESRPane.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void draw(Canvas toolBar) {
  Context2d context2d = toolBar.getContext2d();
  drawButtons(new GraphicsContext(context2d));
}