在p5.js中,如何使drawingContext只影响一个形状?

huangapple go评论79阅读模式
英文:

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. 在p5.js中,如何使drawingContext只影响一个形状?

答案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(&quot;#F8B988&quot;);
  noStroke();
  fill(&quot;#f4d402&quot;);
  circle(width / 2, height * 0.6, 100);
  pop(); 
  fill(&quot;black&quot;);
  rect(0, height * 0.6, width, height);
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月7日 01:25:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定