JSPM

create-graphic

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q22761F
  • License MIT

Draws on a canvas element with the given size and function and returns the result

Package Exports

  • create-graphic

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (create-graphic) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

create-graphic

A simple function for drawing on a canvas element and receiving the result. Useful for programmatically-generated static graphics, WebGL textures (think particles) etc.

Install

npm install create-graphic

Use

import createGraphic from 'create-graphic';

const graphic = createGraphic(256, (context, width, height) => {
  const radius = width / 2;
  const gradient = context.createRadialGradient(radius, radius, 0, radius, radius, radius);
  gradient.addColorStop(0, 'rgba(255, 255, 255, 1)');
  gradient.addColorStop(0.25, 'rgba(255, 255, 255, 1)');
  gradient.addColorStop(0.5, 'rgba(255, 156, 1, 1)');
  gradient.addColorStop(0.6, 'rgba(243, 81, 1, 1)');
  gradient.addColorStop(1, 'rgba(0, 0, 0, 1)');
  context.fillStyle = gradient;
  context.fillRect(0, 0, width, width);
});

The draw function will return the canvas context by default, but you can return anything to override this. The example below also demonstrates defining both width and height, and using the draw function context as the, um, context when not using an arrow function.

import createGraphic from 'create-graphic';

const graphic = createGraphic([400, 200], function(context, width, height) {
  this.fillStyle = '#C0FFEE';
  this.fillRect(0, 0, width, height);
  return this.canvas.toDataURL();
});