简体   繁体   English

如何将javascript中的椭圆背景设置为图像?

[英]How to set the background of an ellipse in javascript to an image?

I'm using javascript with the p5 library and currently I only know how to use the fill() function to fill an ellipse with a color.我将 javascript 与 p5 库一起使用,目前我只知道如何使用 fill() function 用颜色填充椭圆。 Is there a way to set the ellipse's background to an image?有没有办法将椭圆的背景设置为图像?

Rather that setting a background you could try using the mask() method combined with the ellipse() method:而不是设置背景,您可以尝试将mask()方法与ellipse()方法结合使用:

 let photo, maskImage function preload() { photo = loadImage('https://picsum.photos/300/200') } function setup() { createCanvas(600, 200) background(125) image(photo, 0, 0); maskImage = createGraphics(300, 200) maskImage.ellipse(150, 100, 250, 150) photo.mask(maskImage) image(photo, 300, 0) }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>

Instead of drawing an ellipse and setting the background, you can instead draw the image directly using the image() method.无需绘制椭圆并设置背景,您可以直接使用image()方法绘制图像。

If you want your image to be elliptic, and it isn't yet, try using a program like GIMP .如果您希望图像是椭圆的,但现在还不是,请尝试使用GIMP之类的程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM