英文:
In p5.js, how can you make drawingContext only affect one shape?
问题
drawingContext.shadowBlur = 50;
drawingContext.shadowColor = color("#F8B988");
noStroke();
fill("#f4d402");
circle(width/2, height*0.60, 100);
fill("black");
rect(0, height*.60, 0, width);
如何让drawingContext 只影响圆而不影响矩形?
我尝试将矩形放在不同的函数中,但仍然没有解决问题。:(
英文:
drawingContext.shadowBlur = 50;
drawingContext.shadowColor = color("#F8B988")
noStroke()
fill("#f4d402");
circle(width/2, height*0.60, 100);
fill("black")
rect(0, height*.60, 0, width)
How can I make drawingContext only affect the circle and not the rect?
I tried putting rect in a different function, but it still didn't fix.
答案1
得分: 1
使用push()
和pop()
。
引用文档:
push()
函数保存当前的绘图样式设置和变换,而pop()
则恢复这些设置。请注意,这些函数总是一起使用。它们允许您更改样式和变换设置,然后返回到之前的状态。
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
push();
drawingContext.shadowBlur = 50;
drawingContext.shadowColor = color("#F8B988");
noStroke();
fill("#f4d402");
circle(width / 2, height * 0.6, 100);
pop();
fill("black");
rect(0, height * 0.6, width, height);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.min.js"></script>
英文:
Use push()
and pop()
.
Quoting docs:
> The push() function saves the current drawing style settings and
> transformations, while pop() restores these settings. Note that these
> functions are always used together. They allow you to change the style
> and transformation settings and later return to what you had.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
push();
drawingContext.shadowBlur = 50;
drawingContext.shadowColor = color("#F8B988");
noStroke();
fill("#f4d402");
circle(width / 2, height * 0.6, 100);
pop();
fill("black");
rect(0, height * 0.6, width, height);
}
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.min.js"></script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论