How to use the canvas.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 WizardL / NekoPost / backend / canvas / index.js View on Github external
// Measure text width height.
  const mainStringBox = StringWidthHeight(mainString, fontFamilyKey, mainFontSize)
  const signStringBox = StringWidthHeight(signString, fontFamilyKey, signFontSize)

  // Set canvas width and height.
  if (minWidth < mainStringBox.width) {
    minWidth = mainStringBox.width
  }
  if (minHeight < mainStringBox.height + signStringBox.height) {
    minHeight = mainStringBox.height + signStringBox.height
  }
  minWidth += padding * 2
  minHeight += padding * 2

  // Create Canvas object.
  const canvas = new Canvas(minWidth, minHeight)
  const ctx = canvas.getContext('2d')

  // Set background color.
  if (customTheme)
    ctx.fillStyle = customTheme['background']
  else
    ctx.fillStyle = themes[themeKey]['background']
  ctx.fillRect(0, 0, minWidth, minHeight)

  // Set current text baseline used when drawing text.
  ctx.textBaseline = 'top'

  // Drawing text.
  if (customTheme)
    ctx.fillStyle = customTheme['text']
  else
github ElementsProject / filebazaar / src / lib / preview / image.js View on Github external
const pixelate = async path => {
  // load original image
  const img  = new Image
      , buff = img.src = await fs.readFile(path)

  // make canvas for preview image
  const canvas     = new Canvas(img.width, img.height)
      , ctx        = canvas.getContext('2d')

  ctx.imageSmoothingEnabled = false
  ctx.patternQuality = 'fast'

  // resize left-half to 0.1x, then re-enlarge
  const scaledImg  = new Image
  scaledImg.src = await toBuff(gm(buff).crop(img.width/2, img.height, 0, 0).resize(Math.min(img.width*0.1, 80)), 'PNG')
  ctx.drawImage(img, 0, 0, img.width, img.height)
  ctx.drawImage(scaledImg, 0, 0, img.width/2, img.height)

  // add red separator line
  ctx.beginPath()
  ctx.lineWidth = (img.height*0.002)
  ctx.strokeStyle = 'red'
  ctx.moveTo(img.width/2-ctx.lineWidth/2, 0)
github anandanand84 / draw-candlestick / index.js View on Github external
module.exports = function drawCandleStick(input) {
    var width  = 400;
    var height  = 400;

    var Canvas = require('canvas').Canvas
        , Image = Canvas.Image
        , canvas = new Canvas(width, height)
        , ctx = canvas.getContext('2d');

        console.log(Canvas);

    var d3 = require('d3');
    var d3Scale = require("d3-scale")

    var ctx = canvas.getContext('2d');
    ctx.strokeRect(0,0, width, height);
    ctx.translate(0, height);
    ctx.scale(1,-1);

    
    var y = d3Scale.scaleLinear()
      .domain([d3.min(input.low), d3.max(input.high)])
      .range([50, height-50]);
github anandanand84 / draw-candlestick / index.js View on Github external
module.exports = function drawCandleStick(input) {
    var width  = 400;
    var height  = 400;

    var Canvas = require('canvas').Canvas
        , Image = Canvas.Image
        , canvas = new Canvas(width, height)
        , ctx = canvas.getContext('2d');

        console.log(Canvas);

    var d3 = require('d3');
    var d3Scale = require("d3-scale")

    var ctx = canvas.getContext('2d');
    ctx.strokeRect(0,0, width, height);
    ctx.translate(0, height);
    ctx.scale(1,-1);

    
    var y = d3Scale.scaleLinear()
      .domain([d3.min(input.low), d3.max(input.high)])
      .range([50, height-50]);
   
    
    let barsCount   = input.close.length;
github Xetera / hifumi / bot / src / services / image.ts View on Github external
img.onload = () => {
    const canvas = new Canvas(img.width, img.height);
    const ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);
    res(canvas);
  };
  img.onerror = rej;
github WizardL / NekoPost / backend / canvas / awwwesome.js View on Github external
'use strict'

import { Canvas, Image, PNGStream } from 'canvas'

const canvas = new Canvas(233, 889)
const context = canvas.getContext('2d')

function random_rect(rect_no) {
  context.font = '30px Roboto'
  context.fillText('Lorem ipsum', 50, 100, 2)
}

const genFrom = (min, max) => {
  return Math.floor(Math.random() * (Math.floor(max) - Math.ceil(min) + 1)) + Math.ceil(min)
}

export default (str) => {
  background_random(genFrom(1, 4))
  random_rect(genFrom(2, 3))
  big_rect_with_words(str)
}
github WizardL / NekoPost / backend / canvas / index.js View on Github external
function StringWidthHeight(string, fontFamilyKey, fontSize) {
  const tmpCanvas = new Canvas(10, 10)
  const tmpCtx = tmpCanvas.getContext('2d')
  tmpCtx.textBaseline = 'top'
  tmpCtx.font = fontSize + 'px \'' + fonts[fontFamilyKey] + '\''
  const tmpBox = tmpCtx.measureText(string)
  const result = {
    'width': tmpBox.width,
    'height': tmpBox.actualBoundingBoxDescent - tmpBox.actualBoundingBoxAscent
  }
  return result
}
github akira-cn / node-canvas-webgl / lib / canvas.js View on Github external
ctx.texImage2D = function (...args) {
        let pixels = args[args.length - 1];
        if(pixels._image) pixels = pixels._image;
        if(pixels instanceof Image) {
          const canvas = new Canvas(pixels.width, pixels.height);
          canvas.getContext('2d').drawImage(pixels, 0, 0);
          args[args.length - 1] = canvas;
        }
        return _tetImage2D.apply(this, args);
      };
      this.__gl__ = ctx;
github konvajs / konva / konva-node / index.js View on Github external
Konva.Util.createCanvasElement = () => {
  const node = new canvas.Canvas();
  node.style = {};
  return node;
};
github arcanis / colibrijs / index.js View on Github external
var createCanvas = function () {

        if (typeof document !== 'undefined')
            return document.createElement('canvas');

        if (typeof require !== 'undefined')
            return new (require('canvas').Canvas)();

        throw new Error('Canvas creation failed');

    };