Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CanvasPattern from CanvasElement returns transparent #1010

Closed
sovervo opened this issue Mar 2, 2025 · 0 comments · Fixed by #1013
Closed

CanvasPattern from CanvasElement returns transparent #1010

sovervo opened this issue Mar 2, 2025 · 0 comments · Fixed by #1013

Comments

@sovervo
Copy link

sovervo commented Mar 2, 2025

const { promises } = require('node:fs')
const { join } = require('node:path')
const { createCanvas, loadImage, Image } = require('./index.js')

const width = 500
const height = 500
const canvas = createCanvas(width, height)
const ctx = canvas.getContext('2d')


async function main() {
  const catImage = await loadImage("./img.jpg")

  const tempCanvas = createCanvas(width, height);
  const tempCtx = tempCanvas.getContext('2d');

  tempCtx.drawImage(catImage, 0, 0, catImage.width, catImage.height);

  const pattern = ctx.createPattern(catImage, "repeat");
  const pattern2 = ctx.createPattern(tempCanvas, "repeat");

  ctx.fillStyle = pattern;
  ctx.fillRect(0, 0, width/2, height);

  ctx.fillStyle = pattern2;
  ctx.fillRect(width/2, 0, width/2, height);

  const output2 = await canvas.encode('png')
  await promises.writeFile('res.png', output2)
}

main()

result:

Image

expected result:

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant