Search in sources :

Example 1 with ScratchPad

use of com.ait.lienzo.client.core.util.ScratchPad in project kie-wb-common by kiegroup.

the class LienzoCanvasExport method layerToDataURL.

private static String layerToDataURL(final com.ait.lienzo.client.core.shape.Layer layer, final DataURLType dataURLType, final int x, final int y, final int width, final int height, final String bgColor) {
    ScratchPad scratchPad = layer.getScratchPad();
    if (null != bgColor) {
        scratchPad.getContext().setFillColor(bgColor);
        scratchPad.getContext().fillRect(x, y, width, height);
    }
    layer.drawWithTransforms(scratchPad.getContext(), 1, new BoundingBox(x, y, width, height));
    final String data = scratchPad.toDataURL(dataURLType, 1);
    scratchPad.clear();
    return data;
}
Also used : ScratchPad(com.ait.lienzo.client.core.util.ScratchPad) BoundingBox(com.ait.lienzo.client.core.types.BoundingBox)

Example 2 with ScratchPad

use of com.ait.lienzo.client.core.util.ScratchPad in project lienzo-core by ahome-it.

the class WiresConnectorControlImpl method getIndexForSelectedSegment.

public static int getIndexForSelectedSegment(final WiresConnector connector, final int mouseX, final int mouseY, final Point2DArray oldPoints) {
    final NFastStringMap<Integer> colorMap = new NFastStringMap<>();
    final AbstractDirectionalMultiPointShape<?> line = connector.getLine();
    final ScratchPad scratch = line.getScratchPad();
    scratch.clear();
    final PathPartList path = line.getPathPartList();
    int pointsIndex = 1;
    String color = MagnetManager.m_c_rotor.next();
    colorMap.put(color, pointsIndex);
    final Context2D ctx = scratch.getContext();
    final double strokeWidth = line.getStrokeWidth();
    ctx.setStrokeWidth(strokeWidth);
    final Point2D absolutePos = connector.getLine().getComputedLocation();
    final double offsetX = absolutePos.getX();
    final double offsetY = absolutePos.getY();
    Point2D pathStart = new Point2D(offsetX, offsetY);
    Point2D segmentStart = pathStart;
    for (int i = 0; i < path.size(); i++) {
        final PathPartEntryJSO entry = path.get(i);
        NFastDoubleArrayJSO points = entry.getPoints();
        switch(entry.getCommand()) {
            case PathPartEntryJSO.MOVETO_ABSOLUTE:
                {
                    final double x0 = points.get(0) + offsetX;
                    final double y0 = points.get(1) + offsetY;
                    final Point2D m = new Point2D(x0, y0);
                    if (i == 0) {
                        // this is position is needed, if we close the path.
                        pathStart = m;
                    }
                    segmentStart = m;
                    break;
                }
            case PathPartEntryJSO.LINETO_ABSOLUTE:
                {
                    points = entry.getPoints();
                    final double x0 = points.get(0) + offsetX;
                    final double y0 = points.get(1) + offsetY;
                    final Point2D end = new Point2D(x0, y0);
                    if (oldPoints.get(pointsIndex).equals(segmentStart)) {
                        pointsIndex++;
                        color = MagnetManager.m_c_rotor.next();
                        colorMap.put(color, pointsIndex);
                    }
                    ctx.setStrokeColor(color);
                    ctx.beginPath();
                    ctx.moveTo(segmentStart.getX(), segmentStart.getY());
                    ctx.lineTo(x0, y0);
                    ctx.stroke();
                    segmentStart = end;
                    break;
                }
            case PathPartEntryJSO.CLOSE_PATH_PART:
                {
                    final double x0 = pathStart.getX() + offsetX;
                    final double y0 = pathStart.getY() + offsetY;
                    final Point2D end = new Point2D(x0, y0);
                    if (oldPoints.get(pointsIndex).equals(segmentStart)) {
                        pointsIndex++;
                        color = MagnetManager.m_c_rotor.next();
                        colorMap.put(color, pointsIndex);
                    }
                    ctx.setStrokeColor(color);
                    ctx.beginPath();
                    ctx.moveTo(segmentStart.getX(), segmentStart.getY());
                    ctx.lineTo(x0, y0);
                    ctx.stroke();
                    segmentStart = end;
                    break;
                }
            case PathPartEntryJSO.CANVAS_ARCTO_ABSOLUTE:
                {
                    points = entry.getPoints();
                    final double x0 = points.get(0) + offsetX;
                    final double y0 = points.get(1) + offsetY;
                    final Point2D p0 = new Point2D(x0, y0);
                    final double x1 = points.get(2) + offsetX;
                    final double y1 = points.get(3) + offsetY;
                    final double r = points.get(4);
                    final Point2D p1 = new Point2D(x1, y1);
                    final Point2D end = p1;
                    if (p0.equals(oldPoints.get(pointsIndex))) {
                        pointsIndex++;
                        color = MagnetManager.m_c_rotor.next();
                        colorMap.put(color, pointsIndex);
                    }
                    ctx.setStrokeColor(color);
                    ctx.beginPath();
                    ctx.moveTo(segmentStart.getX(), segmentStart.getY());
                    ctx.arcTo(x0, y0, x1, y1, r);
                    ctx.stroke();
                    segmentStart = end;
                    break;
                }
        }
    }
    final BoundingBox box = connector.getLine().getBoundingBox();
    // Keep the ImageData small by clipping just the visible line area
    // But remember the mouse must be offset for this clipped area.
    final int sx = (int) (box.getX() - strokeWidth - offsetX);
    final int sy = (int) (box.getY() - strokeWidth - offsetY);
    final ImageData backing = ctx.getImageData(sx, sy, (int) (box.getWidth() + strokeWidth + strokeWidth), (int) (box.getHeight() + strokeWidth + strokeWidth));
    color = BackingColorMapUtils.findColorAtPoint(backing, mouseX - sx, mouseY - sy);
    return null != color ? colorMap.get(color) : -1;
}
Also used : NFastStringMap(com.ait.tooling.nativetools.client.collection.NFastStringMap) ScratchPad(com.ait.lienzo.client.core.util.ScratchPad) PathPartEntryJSO(com.ait.lienzo.client.core.types.PathPartEntryJSO) NFastDoubleArrayJSO(com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO) Context2D(com.ait.lienzo.client.core.Context2D) Point2D(com.ait.lienzo.client.core.types.Point2D) ImageData(com.ait.lienzo.client.core.types.ImageData) BoundingBox(com.ait.lienzo.client.core.types.BoundingBox) PathPartList(com.ait.lienzo.client.core.types.PathPartList)

Example 3 with ScratchPad

use of com.ait.lienzo.client.core.util.ScratchPad in project lienzo-core by ahome-it.

the class WiresConnectionControlImpl method onMoveStart.

@Override
public void onMoveStart(final double x, final double y) {
    final Point2D points = getControlNode().getComputedLocation();
    m_startX = points.getX();
    m_startY = points.getY();
    final ScratchPad scratch = m_manager.getLayer().getLayer().getScratchPad();
    m_shapesBacking = BackingColorMapUtils.drawShapesToBacking(m_manager.getLayer().getChildShapes(), scratch, null, m_shape_color_map);
    m_connector.getLine().getOverLayer().getContext().createImageData(m_shapesBacking);
    final WiresConnection connection = getConnection();
    m_initialAutoConnect = connection.isAutoConnection();
    // set it to false while dragging
    connection.setAutoConnection(false);
    m_initial_magnet = connection.getMagnet();
    if (null != m_initial_magnet) {
        m_magnets = connection.getMagnet().getMagnets();
        m_magnetsBacking = m_manager.getMagnetManager().drawMagnetsToBack(m_magnets, m_shape_color_map, m_magnet_color_map, scratch);
    }
    // always null when drag start and reset the offsets (they may already be 0)
    connection.setMagnet(null);
    connection.setXOffset(0);
    connection.setYOffset(0);
    final String colorKey = BackingColorMapUtils.findColorAtPoint(m_shapesBacking, (int) m_startX, (int) m_startY);
    checkAllowAndShowMagnets(colorKey);
}
Also used : Point2D(com.ait.lienzo.client.core.types.Point2D) WiresConnection(com.ait.lienzo.client.core.shape.wires.WiresConnection) ScratchPad(com.ait.lienzo.client.core.util.ScratchPad)

Example 4 with ScratchPad

use of com.ait.lienzo.client.core.util.ScratchPad in project lienzo-core by ahome-it.

the class Scene method toDataURL.

public final String toDataURL(final DataURLType mimetype) {
    if (LienzoCore.IS_CANVAS_SUPPORTED) {
        final ScratchPad scratch = new ScratchPad(getWidth(), getHeight());
        final Context2D context = scratch.getContext();
        final NFastArrayList<Layer> layers = getChildNodes();
        BoundingBox bbox = getStorageBounds();
        if (null == bbox) {
            final Viewport viewport = getViewport();
            if (null != viewport) {
                bbox = viewport.getStorageBounds();
            }
        }
        if (null != layers) {
            final int size = layers.size();
            final IPathClipper clip = getPathClipper();
            if ((null != clip) && (clip.isActive())) {
                context.save();
                clip.clip(context);
            }
            for (int i = size - 1; i >= 0; i--) {
                final Layer layer = layers.get(i);
                if ((null != layer) && (layer.isVisible())) {
                    layer.drawWithTransforms(context, 1, bbox);
                }
            }
            if ((null != clip) && (clip.isActive())) {
                context.restore();
            }
        }
        return scratch.toDataURL(mimetype, 1.0);
    } else {
        return "data:,";
    }
}
Also used : Context2D(com.ait.lienzo.client.core.Context2D) ScratchPad(com.ait.lienzo.client.core.util.ScratchPad) BoundingBox(com.ait.lienzo.client.core.types.BoundingBox)

Example 5 with ScratchPad

use of com.ait.lienzo.client.core.util.ScratchPad in project lienzo-core by ahome-it.

the class Scene method toDataURL.

// package protected
final String toDataURL(final DataURLType mimetype, final Layer background) {
    if (LienzoCore.IS_CANVAS_SUPPORTED) {
        final ScratchPad scratch = new ScratchPad(getWidth(), getHeight());
        final Context2D context = scratch.getContext();
        final NFastArrayList<Layer> layers = getChildNodes();
        BoundingBox bbox = getStorageBounds();
        if (null == bbox) {
            final Viewport viewport = getViewport();
            if (null != viewport) {
                bbox = viewport.getStorageBounds();
            }
        }
        if (null != layers) {
            final int size = layers.size();
            if (null != background) {
                background.drawWithTransforms(context, 1, bbox);
            }
            final IPathClipper clip = getPathClipper();
            if ((null != clip) && (clip.isActive())) {
                context.save();
                clip.clip(context);
            }
            for (int i = size - 1; i >= 0; i--) {
                final Layer layer = layers.get(i);
                if ((null != layer) && (layer.isVisible())) {
                    layer.drawWithTransforms(context, 1, bbox);
                }
            }
            if ((null != clip) && (clip.isActive())) {
                context.restore();
            }
        }
        return scratch.toDataURL(mimetype, 1.0);
    } else {
        return "data:,";
    }
}
Also used : Context2D(com.ait.lienzo.client.core.Context2D) ScratchPad(com.ait.lienzo.client.core.util.ScratchPad) BoundingBox(com.ait.lienzo.client.core.types.BoundingBox)

Aggregations

ScratchPad (com.ait.lienzo.client.core.util.ScratchPad)13 Context2D (com.ait.lienzo.client.core.Context2D)8 BoundingBox (com.ait.lienzo.client.core.types.BoundingBox)6 ImageData (com.ait.lienzo.client.core.types.ImageData)2 Point2D (com.ait.lienzo.client.core.types.Point2D)2 WiresConnection (com.ait.lienzo.client.core.shape.wires.WiresConnection)1 DashArray (com.ait.lienzo.client.core.types.DashArray)1 PathPartEntryJSO (com.ait.lienzo.client.core.types.PathPartEntryJSO)1 PathPartList (com.ait.lienzo.client.core.types.PathPartList)1 NFastDoubleArrayJSO (com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO)1 NFastStringMap (com.ait.tooling.nativetools.client.collection.NFastStringMap)1 UmbrellaException (com.google.gwt.event.shared.UmbrellaException)1