I2Djs-v5
  • Introduction to I2Djs
  • Getting Started
    • Installation
    • Quick Start
    • Resources
  • Guide
    • Canvas: Create-animate
    • SVG: Create-animate
    • WebGL: Create-animate
    • PDF: Create
    • Data Join-Action
    • Animation-Chaining
    • Canvas: Textures & Sprite animation
    • Canvas: Heatmap
    • WebGL: Heatmap
    • WebGL: Custom Shaders
    • Canvas: GeoWorld Map with D3 modules
    • Canvas: Clipping, Masking, Pattern
    • SVG: Clipping, Masking, Pattern
    • I2Djs + Physics Engine - MatterJs
  • API Reference
    • Layers
      • Canvas Layer
      • SVG Layer
      • WebGL Layer
    • Elements API
    • Canvas additional features
    • WebGL additional features
      • Custom Shader
      • Geometry
      • Render Target
      • Texture Object
    • Join Action
    • Animation
    • Chaining
    • Path
    • Events
    • Behaviour
      • Zoom and Pan
      • Drag
  • Examples
    • PDF
    • Canvas
    • SVG
    • WebGL
Powered by GitBook
On this page
  • Layer Creation:
  • Element Creation:
  • Animation:
  1. Guide

WebGL: Create-animate

learn WebGl Layer - Element creation and animation of its attributes.

Create an HTML container with ID to render the graphical context element

<div id="myWebgl"> </div>

Layer Creation:

To create the WebGL layer, invoke the webglLayer method with ContainerID "#myWebgl". It returns webGL-2D renderer instance. For more details on webglLayer please refer : API Reference -> Layers -> WebGL Layer Page.

let renderer = i2d.webglLayer('#myWebgl', {}, {});

Element Creation:

To render elements, first, we need to create the shader group and then add elements to it. Without the shader group, elements won't be rendered. For example, to create a circle, we need to create a circle shader group and then rect elements.

Code block to create rects.

let rectGroup = webGlLayerInstance.createEl({
					el: 'group',
					attr: {
						shaderType: 'circle'
					}
				});

Let's create a circle element.

let rect = rectGroup.createEl({
					el: 'circle':
					attr: {
						cx: 100,
						cy: 100,
						r: 250
					},
					style: {
						fill: 'hsl(35%, 100%, 0%)';
					}
				});

Animation:

Now we will animate circle radius from 50 to 200 and fillStyle from '#ff0000' to '#00ff00' in duration - 500 ms. By specifying loop = 10, animation will be repeated 10 time, with alternate direction and easeInOutSin easing.

circle.animateTo({
    duration: 500,
    loop: 10,
    ease: 'easeInOutSin',
    direction: 'alternate',
    attr: {
        r: 200
    },
    style: {
        fill: '#00ff00'
    }
});
PreviousSVG: Create-animateNextPDF: Create