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);
                    }
        }
    }

  • 相关阅读:
    ss
    Linux vmstat命令实战详解
    【好书摘要】性能优化中CPU、内存、磁盘IO、网络性能的依赖
    mysql 数据库授权(给某个用户授权某个数据库)
    windows下mysql安装包安装
    053(五十五)
    053(五十四)
    053(五十三)
    053(五十二)
    053(五十一)
  • 原文地址:https://www.cnblogs.com/chenzxl/p/14784995.html
Copyright © 2011-2022 走看看