node-i2djs
  • Introduction to node-i2djs
  • Getting Started
    • Installation
    • Quick Start CanvasLayer
    • Quick Start Canvas PDF Layer
    • Examples
  • API Reference
    • canvasLayer
    • canvasPdfLayer
    • Element API
    • Textures
    • Path
    • Join Action
  • Guide
    • Element types
    • Data Join-Actions
    • Canvas: Textures & Sprite animation
    • Canvas: Clipping, Masking, Pattern
Powered by GitBook
On this page
  • Shapes Supported:
  • Circle
  • Line
  • Path
  • Group
  • Image
  • Pixel Manipulation:
  1. Guide

Element types

Canvas is a raster-based graphics rendering API. I2D Canvas provides API's with simple syntax to create different geometrical shapes. It has an internal pseudo-DOM implementation of all basic elements supported by SVG. It also provides pixel manipulation capabilities and also supports events to create more interactive canvas visualizations.

Shapes Supported:

Canvas supports all geometrical primitives which are supported in SVG - rect, circle, line, polyline, polygon, path, group, ellipse.

For example, the below code snippet creates a circle using I2Ds canvas renderer instance. Attr object consists of attributes similar to SVG, whereas style follows Canvas properties.

Circle

let circle = renderer.createEl({
    el: 'circle',
    attr: {
        r: 50,
        cx: renderer.width / 2,
        cy: renderer.height / 2
    },
    style: {
        fillStyle: '#ff0000',
        strokeStyle: '#000000'
    }
})

Line

Below code snippet creates a line with X1, Y1, X2, Y2 attributes with Style as per Canvas syntax.

let line = renderer.createEl({
    el: 'line',
    attr: {
        x1: 10,
        y1: 100,
        x2: 100,
        y2: 100,
    },
    style: {
        strokeStyle: '#00ff00',
        lineWidth: 5
    }
})

Path

let path = renderer.createEl({
    el: 'path',
    attr: {
        d: "M150 0 L75 200 L225 200 Z"
    },
    style: {
        strokeStyle: '#ff0000'
    }
})

Group

Similar to SVG, I2Djs provides a group element under Canvas API. Group Element helps in writing common attributes along with styles to all its children.

For Example: All the children will be translated by [100, 100] with fill = '#00ff00'

let group = renderer.createEl({
    el: 'group',
    attr: {
        transform: {
            translate: [100, 100]
        }
    },
    style: {
        fillStyle: '#00ff00'
    }
});

Image

I2Djs canvas image API provides special functionality for manipulating pixels of the image - clip, and pixels.

Clip:

Used to clip the image as per 'sx', 'sy', 'swidth', 'sheight' parameters. In the below example it is used in clipping the sprite image.

var img = renderer_.createEl({ 
    el: 'image', 
    attr: { 
        src: '../images/boySprite.png', 
        width: 500, 
        height: 700, 
        x: 400, 
        y: 0, 
        clip: { 
            sx: 0, 
            sy: 0, 
            swidth: 108, 
            sheight: 140 
        }
    }
});

Pixel Manipulation:

Used to manipulate the pixels of the image. Accepts function as a value, which will be invoked by passing pixelData as an argument. On execution, the function has to return the manipulated pixelData Object.

var img = renderer_.createEl({ 
    el: 'image', 
    attr: { 
        src: '../images/boySprite.png', 
        width: 500, 
        height: 700, 
        x: 400, 
        y: 0, 
        pixels: function (pixelData) { 
            localPixelData = pixelData.data; 
            return pixelData; 
        } 
    } 
}); 
PreviousGuideNextData Join-Actions

Last updated 2 years ago

Example:

Boy Clipping