简体   繁体   中英

HTML5 canvas color

I have two blocks in HTML5 canvas.

  1. Blue Block ie fixed in the canvas
  2. Yellow Block that can be dragged with mouse.

When someone moves the yellow block over blue block, I want to change the color of overlapping or intersection regions to green. (please see attached image to have clear idea)

在此处输入图片说明

Since blue + yellow = green , is there any way to achieve this by changing the opacity level of blocks or I have to search for the overlapping area of the two blocks and display green block in that area or is there any other way?

I would like to know what is the best approach to achieve this?

Have a look at canvas globalCompositeOperation . The lighter composite type seems to fit what you're after.

You could use 3 elements:

  • Yellow bottom: Opacity 1
  • Yellow top: Opacity 0.x, same dimensions as the bottom one
  • Blue: Full opacity between the yellow divs

Example on jsFiddle

This is far from done, but maybe a step in the right direction.

EDIT: I noticed too late that you requested it on canvas, but the principe should be the same there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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