当前位置:网站首页>Canvas advanced functions (medium)
Canvas advanced functions (medium)
2022-06-10 20:34:00 【Xia'an】
canvas Advanced features ( in )
canvas Advanced features ( in )
In this paper , You will learn Canvas Some of the more advanced features . This article will show you how to synthesize 、 Create shadows to make the graphics look more realistic and interesting . This article is very exciting , I hope these contents can broaden your horizon , Help you learn the advanced functions of canvas .
1. synthesis
canvas Advanced features ( in ) It does include a lot of content , Congratulations on having learned this knowledge . In this section , We will learn some easy things —— synthesis , They are not complicated , And it's fun . In short , Composition is to combine multiple visual elements into one visual element .
Everything you paint on the canvas is composited , This means that everything drawn will be merged with the existing elements that have been drawn . This is actually a basic synthesis , Just superimpose some content on others . I am going to introduce the synthesis of these aspects , But now let's take a look at the simplest compositing method in the canvas , namely globalAlpha attribute .
Be careful : The two global compositing attributes described in this section affect 2D Render the drawing effect of the context . Make it clear , That is, modifying the global composition attribute will affect all the contents drawn after modification .
1.1 Global alpha value
Before drawing on the canvas , It will apply a and globalAlpha The alpha value that the attribute matches . Assign to globalAlpha The value of must be at 0.0( Completely transparent ) And 1.0( Opaque ) Between , The default value is 1.0. In short ,globalAlpha Attributes affect the transparency of the object to be painted . for example , You can draw a translucent square as follows :
context.fillStyle = "rgb(63, 169, 245)";
context.fillRect(50, 50, 100, 100);
context.globalAlpha = 0.5;
context.fillStyle = "rgb(255, 123, 172)";
context.fillRect(100, 100, 100, 100);
Because we only set it after drawing the blue square globalAlpha Attribute , So only pink squares are affected by alpha values . The result is that a small piece of the blue square in the back is slightly displayed through the pink square in the front .
Now? , By giving fillStyle Set a containing less than 1 Alpha value of rgba value , You can get the same effect . Here's the difference ,globalAlpha The global alpha value is set , This value will be applied later rgba Color value and other alpha values are referenced . for example , If globalAlpha by 0.5, You applied it again fillstyle( It has an alpha value of 0.5 Of rgba), So the alpha value of the result is actually 0.25.2D Global alpha value of the rendering context (0.5) Acts as the base for calculating other alpha values (0.5*0.5=0.25).
1.2 Synthesis operation
As described at the beginning of this section , Even if it's brand new 2D The dyadic context also uses synthesis from the beginning . You may not have noticed this , Because the synthesis method used at this time can get the desired results : One figure superimposed on another . This composition is called source over target , The source is a new drawing , The target is something that may have been mapped 2D Rendering context . We know , This is because 2D Rendering context globalCompositeOperation The default value of the property is source-over, And this attribute defines the pair 2D The type of composition performed by all painted shapes on the rendering context (11 One of the two options ). It must be noted that , Depending on the order of assignment globalCompositeOperation All the values of may relate to one of the source or target ( It depends on the order ), Not both . for example ,source-over yes ( The source overrides the target ) For short , The goal is implicit , Because it doesn't need to specify... In the value ( The source must be drawn on something ).
Let's get to know globalCompositeOperation Supported by 11 A choice . Use the following code as a template , You can learn every synthesis operation . The blue square is the target , And the pink square is the source . The reason we only use blue and pink instead of other colors is that they can better show the effect of the compositing operation :
context.fillStyle = "rgb(63, 169, 245)";
context.fillRect(50, 50, 100, 100);
context.globalCompositeoperation = "source-over";
context.fillStyle = "rgb(255,123,172)";
context.fillRect(100, 100, 100, 100);
Be careful : Some browsers don't support all
globalCompositeOperationvalue .
source-over
This is the default , It represents the drawing ( Source ) Will be drawn on the existing canvas ( The goal is ) above :
context.globalCompositeOperation = "source-over";
The effect is exactly the same as the drawing effect learned so far .
destination-over
The value of this operation is the opposite of the previous value , So now the target is drawn on top of the source :
context.globalCompositeOperation = "destination-over";
The effect is just the opposite of the previous operation .
source-atop
This operation will draw the source on top of the target , But both are opaque in the overlapping area . Targets drawn in other locations are opaque , But the source is transparent .
destination-atop
This operation is related to source-atop contrary , The target is drawn on top of the source , Where both are opaque in the overlapping area , But sources drawn elsewhere are opaque , And the goal becomes transparent .
source-in
Draw only the source in the area where the source and target overlap . And the non overlapping parts become transparent .
destination-in
This operation is related to source-in contrary , Keep the target in the area where the source and target overlap . And the non overlapping parts become transparent .
source-out
Draw the source on an area that does not overlap the target . Other parts become transparent .
destination-out
Keep the target on an area that does not overlap the source . Other parts become transparent .
lighter
This value is independent of order , If the source and target overlap , Add the color values of the two . The maximum value of the obtained color value is 255, The result is white .
copy
This value is independent of order , Draw only the source , Cover the target .
xor( Exclusive or )
This value is independent of order , Only non overlapping source and target areas are drawn . All overlaps become transparent .
All in all , These compositing operations enable you to achieve some interesting effects when you need to draw some complex graphics . There are some operations ( Such as destination-out ) It is useful when erasing some non rectangular areas on the canvas : for example , Use a circle as the source .
2. shadow
Everyone likes nice shadow effects , They could be Adobe Photoshop The most widely used effect in , And often in Web And graphic design . If the operation is correct , They actually increase the realism of the image . However , If the operation is improper , They can also completely destroy an image .
Creating shadow effects on the canvas is relatively simple , It can go through 4 Global attributes to control . These properties are shadowBlur、shadowOffsetX、shadowOffsetY and shadowColor. We will begin to explain these attributes one by one . By default ,2D Rendering context will not draw shadow effect , because shadowBlur、shadowOffsetX and shadowOffsetY Set to 0, and shadowColor Is set to transparent black . The only way to create a shadow effect is to shadowColor Change to opaque value , At the same time shadowBlur、shadowOffsetX or shadowOffsetY Are set to non 0 value :
context.shadowBlur = 20;
context.shadowColor = "rgb(0 ,0, 0)";
context.fillRect(50, 50, 100, 100);
In this case , Set... For shadows 20 Blur value of pixel , And set its color to completely opaque black . The offset value of the shadow is x Axis and y The axis direction remains the default value 0. It is important to note that , Even with opaque black , But because of the fuzzy effect , This shadow still has some transparency on the border .
modify shadowBlur、shadowOffsetX or shadowOffsetY attribute , You can create different shadow effects :
context.shadowBlur = 0;
context.shadowOffsetX = 10;
context.shadowOffsetY = 10;
context.shadowColor = "rgba(100, 100, 100, 0.5)"; // Transparent grey
context.fillRect(200, 50, 100, 100);
Change the blur to 0, Create a clear shadow effect , And slightly to the right , You get a different shadow effect . Use the... Mentioned in the previous section rgba Color values will shadowColor Set to transparent light gray , To achieve a more dazzling effect .
Canvas shadows support all graphics , So you can create a shadow effect on the circle or other shapes you draw . You can even change the color to any fancy value :
context.shadowColor = "rgb(255,0,0)"; // Red
context.shadowBlur = 50;
context.shadowOffsetX = 0;
context.shadowOffsetY = 0;
context.beginPath();
context.arc(400, 100, 50, 0, Math.PI * 2, false);
context.closePath();
context.fill();
This code will get a circle , It has a bright red shadow effect behind it .
By combining various blur and color values , We can achieve some effects that are completely independent of shadows . for example , Use a blurred yellow shadow to create a lighting effect around an object , Such as the sun or a luminous body .
边栏推荐
- 割舍绳子/整数分割
- Fs4521 constant voltage linear charging IC
- During the college entrance examination this year, all examination sites were in good order, and no sensitive cases affecting safety occurred
- 老程序员说:别再直译这大千世界了,开发人员应该回归程序设计
- Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE tha
- AttributeError: module ‘collections‘ has no attribute ‘MutableMapping‘
- Does Seata still not support sqlserver?
- canvas 高级功能(上)
- Hm3416h buck IC chip pwm/pfm controls DC-DC buck converter
- 性能测试方案(计划)模板
猜你喜欢

MySQL数据库基础

vulnhub-The Planets: Earth

【观察】昇腾智行:场景驱动,创新先行,为智慧交通按下“加速键”

vulnhub-The Planets: Earth

SBC芯片35584数据手册预调节器翻译

hidden danger? Limited meaning? Can't stop the real cooking Mini kitchenware hot 618

用 Plotly 绘制了几张精湛的图表,美翻了!!

「Bug」问题分析 RuntimeError:which is output 0 of ReluBackward0

Kp522201a adopts SOT23-6 encapsulated 4.5V to 17V input, 2A output, 600kHz synchronous step-down converter

RT-Thread Smart Win10 64位下编译环境的搭建
随机推荐
功耗开发经验分享:设计功耗大板
京东发布基于张量网络加速的大规模、分布式量子机器学习平台TeD-Q
On the development trend of enterprise storage: cold thoughts on open source storage
暗黑破坏神不朽WIKI地址 暗黑破坏神不朽数据库地址分享
Rotated Sorted Array旋转排序数组相关题
Routine solution - the problem of horse walking on the chessboard
Esp8266 system environment setup
4.35v lithium battery charging IC
The new audio infinix machine appears in the Google product library, and Tecno CaMon 19 is pre installed with Android 13
Jiangbolong forestee xp2000 PCIe 4.0 SSD multi encryption function, locking data security
MySQL数据库基础
latex tips 绝对值的竖线 \left|\right|
Mysql database foundation
NFS network mount to create server image
在阿里云国际上使用 OSS 和 CDN 部署静态网站
历时2年442位作者132个机构!Google发布语言模型评价新基准BIG-bench,204个任务全面评价语言模型能力,附论文
力扣1082,1084题解_sql查询类型的题目
Standing at such a time node today, we may have a clearer understanding of the industrial Internet
SBC芯片35584数据手册预调节器翻译
移动端渲染原理浅析