How to use the canvas function in canvas

To help you get started, we’ve selected a few canvas examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github conveyal / r5 / static-images / geobuf-grid.js View on Github external
function writePng (arrbuf, file) {
  let dv = new DataView(arrbuf)
  let width = dv.getInt32(12, true)
  let height = dv.getInt32(16, true)

  let cvs = new Canvas(width, height)
  let ctx = cvs.getContext('2d')
  let data = ctx.createImageData(width, height)

  let array = new Float64Array(arrbuf, 24)

  // first figure out the maximum value
  let max = 1

  let curr = 0

  for (let i = 0; i < array.length; i++) {
    curr += array[i]
    // take a log because there are often order-of-magnitude differences in job density across a region, this avoids creating
    // a black png with a few white spots.
    max = Math.max(Math.log(curr + 1), max)
  }
github Gaya / functional-snake / src / main.js View on Github external
import createCanvas from 'canvas';
import setup from 'engine';
import { GAME_WIDTH, GAME_HEIGHT, GAME_SCALE } from 'constants/game';

const canvas = createCanvas(GAME_WIDTH * GAME_SCALE, GAME_HEIGHT * GAME_SCALE);

// set focus to window
window.focus();

// run program
setup(canvas);
github superwf / slider-puzzle-captcha / src / server / puzzle / index.js View on Github external
import Canvas from 'canvas'
import config from '../../config'

const {slotRadius, puzzleSide, bgWidth, bgHeight} = config
const sideLength = puzzleSide + slotRadius * 2
const canvas = new Canvas(bgWidth, bgHeight)
const ctx = canvas.getContext('2d')
let img = new Canvas.Image()

const img1 = new Canvas.Image()
const canvas1 = new Canvas(sideLength, bgHeight)
const ctx1 = canvas1.getContext('2d')

const puzzle = (data, shape, position) => {
  ctx.clearRect(0, 0, bgWidth, bgHeight)
  ctx1.clearRect(0, 0, bgWidth, bgHeight)
  img.src = data
  ctx.drawImage(img, 0, 0)
  shape(ctx, () => {
    ctx.save()
    ctx.globalCompositeOperation = 'destination-in'
    ctx.fill()
    ctx.restore()

    // 外侧阴影
    ctx.save()
    ctx.shadowBlur = 4
github rni-l / canvas-Image-processing / lib / util / getCode.js View on Github external
import Canvas from 'canvas'
// canvas
const boxW = 200,
  boxH = 80

const canvas = new Canvas(boxW, boxH),
  ctx = canvas.getContext('2d')

// 线长度的范围
const maxL = 80,
  minL = 30,
  maxW = 5, // 线厚度
  minW = 1
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'i', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'I', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
let txtArr = null

function getRandom(max, min) {
  return parseInt(Math.random() * (max - min + 1) + min, 10)
}

// 生成线
function drawLine() {
github superwf / slider-puzzle-captcha / src / server / puzzle / index.js View on Github external
import Canvas from 'canvas'
import config from '../../config'

const {slotRadius, puzzleSide, bgWidth, bgHeight} = config
const sideLength = puzzleSide + slotRadius * 2
const canvas = new Canvas(bgWidth, bgHeight)
const ctx = canvas.getContext('2d')
let img = new Canvas.Image()

const img1 = new Canvas.Image()
const canvas1 = new Canvas(sideLength, bgHeight)
const ctx1 = canvas1.getContext('2d')

const puzzle = (data, shape, position) => {
  ctx.clearRect(0, 0, bgWidth, bgHeight)
  ctx1.clearRect(0, 0, bgWidth, bgHeight)
  img.src = data
  ctx.drawImage(img, 0, 0)
  shape(ctx, () => {
    ctx.save()
    ctx.globalCompositeOperation = 'destination-in'
    ctx.fill()
github Jezzamonn / fourier-shape-transform / js / gif-gen.js View on Github external
function generateFrames(controller, options, outDirectory) {
    const {width, height, fps, numSubFrames, length} = options;
    const canvas = new Canvas(width, height);
    const context = canvas.getContext('2d');
    controller.update(0);
    
    const dt = (1 / fps);
    const subFrameTime = dt / numSubFrames;

    mkdirp(outDirectory);
    
    let frameNumber = 0;
    for (let time = 0; time < length; time += dt) {
        const subframes = [];
        for (let i = 0; i < numSubFrames; i ++) {
            renderFrame(context, controller, width, height);
            subframes.push(context.getImageData(0, 0, width, height));
    
            controller.update(subFrameTime);
github superwf / slider-puzzle-captcha / src / server / puzzle / background.js View on Github external
import Canvas from 'canvas'
import config from '../../config'

const img = new Canvas.Image()
const canvas = new Canvas(config.bgWidth, config.bgHeight)
const ctx = canvas.getContext('2d')

const puzzledBg = (data, shape) => {
  ctx.clearRect(0, 0, config.bgWidth, config.bgHeight)
  img.src = data
  ctx.drawImage(img, 0, 0)
  shape(ctx, () => {
    ctx.save()
    ctx.fillStyle = 'rgba(0, 0, 0, 0.3)'
    ctx.fill()
    ctx.restore()

    ctx.save()
    ctx.strokeStyle = 'rgba(0, 0, 0, 0.3)'
    ctx.shadowBlur = 5
    ctx.shadowColor = 'rgba(0, 0, 0, 1)'
github NightCatSama / My-Animat / colors / src / javascripts / index.js View on Github external
import Canvas from 'canvas';

var canvas = new Canvas(document.getElementById('canvas'), {
});
canvas.setImage('dist/images/3.jpg')
canvas.setImage('dist/images/2.jpg', 'colors')