Canvas 全局不透明度和全局图像重叠设置

摘要:
全局不透明度globalAlphaglobalAlpha=1(Default)varcanvas=document.getElementById('canvas')canvas.width=1200;canvas.height=800;varcontext=canvas.getContext("2d");context.globalAlpha=0.7;//设置整个绘制系统的透明度for(vari=

全局不透明度

globalAlpha

globalAlpha = 1 (Default)

var canvas = document.getElementById('canvas')
    canvas.width = 1200;
    canvas.height = 800;
    var context = canvas.getContext("2d");

    context.globalAlpha = 0.7; // 设置整个绘制系统的透明度

    for(var i = 0; i < 100; i++){
      var R = Math.floor(Math.random()*255);
      var G = Math.floor(Math.random()*255);
      var B = Math.floor(Math.random()*255);

      context.fillStyle = `rgb(${R}, ${G}, ${B})`

      context.beginPath();
      context.arc(Math.random()*canvas.width, Math.random()*canvas.height, Math.random()*100, 0, 2*Math.PI);
      context.fill();
    }

全局图像重叠

globleCompositeOperation

globleCompositeOperation = "source-over" (Default)

source-over

source-atop

source-in

source-out


destination-over

destination-atop

destination-in

destination-out


lighter
copy
xor


各参数效果演示代码

image-20211014143155391
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #buttons { 1200px; margin: 10px auto; clear: both;}
    #buttons a {font-size: 18px; display: block; float: left; margin-right: 14px;}
  </style>
</head>
<body>
  <canvas id="canvas"></canvas>
  <div id="buttons">
    <a href="https://tool.4xseo.com/article/89384.html">source-over</a>
    <a href="https://tool.4xseo.com/article/89384.html">source-atop</a>
    <a href="https://tool.4xseo.com/article/89384.html">source-in</a>
    <a href="https://tool.4xseo.com/article/89384.html">source-out</a>
    <a href="https://tool.4xseo.com/article/89384.html">destination-over</a>
    <a href="https://tool.4xseo.com/article/89384.html">destination-atop</a>
    <a href="https://tool.4xseo.com/article/89384.html">destination-in</a>
    <a href="https://tool.4xseo.com/article/89384.html">destination-out</a>
    <a href="https://tool.4xseo.com/article/89384.html">lighter</a>
    <a href="https://tool.4xseo.com/article/89384.html">copy</a>
    <a href="https://tool.4xseo.com/article/89384.html">xor</a>
  </div>
  <script>
    draw('source-over');
    let buttons = document.getElementById('buttons').getElementsByTagName('a');
    for(let i = 0; i < buttons.length; i++) {
      buttons[i].onclick = function() {
        draw(this.text);
        return false;
      }
    }
    function draw(compositeStyle) {
      const canvas = document.getElementById('canvas');
      canvas.width = 1200;
      canvas.height = 800;
      const context = canvas.getContext('2d');
      context.clearRect(0, 0, canvas.width, canvas.height);
      // draw title
      context.font = 'bold 40px Arial';
      context.textAlign = 'center';
      context.textBaseline = 'middle';
      context.fillStyle = '#058'
      context.fillText(`globalCompositeOperation = ${compositeStyle}`, canvas.width / 2, 60);

      // draw a rect
      context.fillStyle = 'blue';
      context.fillRect(300, 150, 500, 500);

      // draw a triangle
      context.globalCompositeOperation = compositeStyle;
      context.fillStyle = 'red';
      context.beginPath();
      context.moveTo(700, 250);
      context.lineTo(1000, 750);
      context.lineTo(400, 750);
      context.closePath();
      context.fill();
    }
  </script>
</body>
</html>

免责声明:文章转载自《Canvas 全局不透明度和全局图像重叠设置》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[PostgreSQL] 图解安装 PostgreSQLkuangbin专题 专题九 连通图 POJ 3694 Network下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Crash监控的简单实现方案

个人博客 http://www.milovetingting.cn Crash监控的简单实现方案 前言 本文从Java层及Native展开,简单记录Android中的Crash监控方案。 Java层Crash Java层的crash监控,可以通过实现Thread.UncaughtExceptionHandler接口,重写uncaughtExceptio...

内存泄漏解析

永远的Singleton 单例的使用在我们的程序中随处可见,因为使用它可以完美的解决我们在程序中重复创建对象的问题,不过可别小瞧它。由于单例的静态特性,使得它的生命周期和应用的生命周期会一样长,所以一旦使用有误,小心无限制的持有Activity的引用而导致内存泄漏。比如,下面的例子。 public class SingletonBad { pri...

html5中的Canvas对图片的一些修改

先展示一下效果: 左边是处理前的原图,右边是经过canvas处理之后的效果。 html代码如下: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title><...

Spring框架系列(二)--装配和创建Bean

  企业日常开发中,几乎都是Spring系的框架,无论是SSM、还是现在大火的SpringBoot,使用最大的目的就是简化开发 基本模块: 核心容器:Beans、Core、Context、SpEL 1. core和beans模块提供了整个框架最基础的部分,包括了IoC(控制反转)和Dependency Injection(依赖注入)。 2. Contex...

WPF学习笔记二 WPF命中测试

概述: WPF中的Canvas是常用的一个绘图控件,可以方便地在Canvas中添加我们需要处理的各种元素如:图片、文字等。但Canvas中元素增加到一定数量,并且有重合的时候,我们如何通过在Canvas中点击鼠标,获得我们想要的元素,然后再对该元素做出相应的控制? 命中测试,可以很好地解决这个问题 本文目的: 使用命中测试,选取Canvas中相应Ele...

Eclipse Code Template 设置自动加注释(转)

Eclipse Code Template 设置自动加注释 设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是所有需设置注释的元素啦。现就每一个元素逐一介绍: 文件(Files)注释标签: /*** @Title: ${fi...