zoukankan      html  css  js  c++  java
  • div给我画条龙

     

    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    
    var image = new Image();
    image.src = "dragon.jpg";
    image.onload = function(){
            canvas.width = image.width;
            canvas.height = image.height;
    
            ctx.drawImage(image,0,0);
    }
    //获取并裁剪画布的点阵信息
    var imageData = ctx.getImageData(0,0,image.width,image.height).data;
    ctx.fillStyle = "#ffffff";
    ctx.fillRect(0,0,image.width,image.height);
    
    var gap = 6;
    
    for (var h = 0; h < image.height; h+=gap) {
        for(var w = 0; w < image.width; w+=gap){
                var position = (image.width * h + w) * 4;
                var r = imageData[position], g = imageData[position + 1], b = imageData[position + 2];
    
                if(r+g+b==0){
                        ctx.fillStyle = "#000";
                        ctx.fillRect(w,h,4,4);
                    }
        }
    }

    现在我们获得了这样一条龙的点阵信息

     

      

    //换成气泡
    var dragonContainer = document.getElementById("container");
    var dragonScale = 2;
    
    for (var h = 0; h < image.height; h+=gap) {
        for(var w = 0; w < image.width; w+=gap){
                var position = (image.width * h + w) * 4;
                var r = imageData[position], g = imageData[position + 1], b = imageData[position + 2];
    
                if(r+g+b==0){
                        var bubble = document.createElement("img");
                        bubble.src = "bubble.png";
                        bubble.setAttribute("class","bubble");
    
                        var bubbleSize = Math.random()*10+20;
                        bubble.style.left = (w*dragonScale-bubbleSize/2) + "px";
                        bubble.style.top = (h*dragonScale-bubbleSize/2) + "px";
                        bubble.style.width = bubble.style.height = bubbleSize+"px";
                        bubble.style.animationDuration = Math.random()*6+4 + "s";
    
                        dragonContainer.appendChild(bubble);
                    }
        }
    }

  • 相关阅读:
    Git在eclipse中的使用
    Git协同开发产生的版本冲突
    git&github-远程库的拉取
    【题解】p6160 [Cnoi2020]向量
    【题解】p2388 阶乘之乘
    友情链接
    O(1)求解自然数异或和
    【题解】uva1104 chips challenge
    【题解】p1809 过河问题
    多步操作产生错误,请检查每一步的状态
  • 原文地址:https://www.cnblogs.com/chenzxl/p/14784995.html
Copyright © 2011-2022 走看看