Skip to content

drawing image fails if ctx.rotate() precision is too deep #1417

Description

@andromika

Issue or Feature

The following function works perfectly in node-canvas 2.4 and below. in 2.5 it simply ignores the image being drawn inside the hexagon shape.

Steps to Reproduce

try the following in both 2.4 and 2.5

const Canvas = require('canvas');
async function Hex(size, picture) {
  let globalOffset = 0
  size = size / 2
  let x = size + 10
  let y = -size

  let hex = new Canvas.createCanvas(size * 2 + 20, size * 2 + 20)
  let c = hex.getContext("2d")
  c.rotate(1.5708)
  c.save();
  c.beginPath();
  c.moveTo(x + size * Math.cos(0), y + size * Math.sin(0));

  for (side = 0; side < 7; side++) {
    c.lineTo(x + size * Math.cos(side * 2 * Math.PI / 6), y + size * Math.sin(side * 2 * Math.PI / 6));
  }

  c.fillStyle = "#ffffff"
  c.fill();
  if (picture) {
    c.clip();
    let pict = await Canvas.loadImage(picture);
    c.rotate(-1.5708)
    c.drawImage(pict, 0, x - size, size * 2, size * 2);
    c.restore()

    c.globalCompositeOperation = 'xor';
    c.shadowOffsetX = 0;
    c.shadowOffsetY = 0;
    c.shadowBlur = 10;
    c.shadowColor = 'rgba(30,30,30,1)';

    c.beginPath();
    for (side = 0; side < 7; side++) {
      c.lineTo(x + size * Math.cos(side * 2 * Math.PI / 6), y + size * Math.sin(side * 2 * Math.PI / 6));
    }
    c.stroke();

    c.globalCompositeOperation = 'destination-atop';

  } else {
    c.shadowColor = "rgba(34, 31, 59, 0.57)"
    c.shadowBlur = 8
  }

  return hex

}

Your Environment

  • Version of node-canvas (output of npm list canvas or yarn list canvas):
    2.5.0 (faulty)
    2.4.0 (working)

  • Environment (e.g. node 4.2.0 on Mac OS X 10.8):
    node 11.14
    ubuntu 16.04

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions