【问题标题】:How to fade out an item in canvas如何淡出画布中的项目
【发布时间】:2015-01-25 21:33:53
【问题描述】:

我的页面中有一个全屏画布。这幅画布上还有一些潜水元素。画布中有一个圆形元素,它随光标在页面中的任何位置移动。但是,当光标到达画布上的 div 元素时,圆形会停留在到达 div 之前画布上的最后一个位置。

演示:JSFIDDLE

当光标在 div 元素上时,有什么方法可以使圆形淡出并在它回到画布时淡入?

除了淡出之外,还有其他效果吗?就像把它变小然后淡出......

这是与圆相关的代码:

function writeMessage(canvas, message, x, y) {
    var context = canvas.getContext('2d');
    context.clearRect(0, 0, canvas.width, canvas.height);

    var pattern = context.createPattern(imageResized, 'no-repeat');//Use imageResized, not imageObj.
    context.fillStyle = pattern;
    context.fill();    

    context.fillStyle = 'black';
    context.beginPath();
    context.arc(x, y, 60, 0, 2 * Math.PI);

}

【问题讨论】:

    标签: javascript jquery css canvas fadeout


    【解决方案1】:

    好吧,您始终可以创建自己的淡入淡出函数,该函数在mouseout(或mouseleave)事件中被调用。这是我为您快速构建的一个:

    function fadeOut(canvas, message, x, y, amount) {    
        var context = canvas.getContext('2d');
        context.clearRect(0, 0, canvas.width, canvas.height);
    
        var pattern = context.createPattern(imageResized, 'no-repeat');
        context.fillStyle = pattern;
        context.fill();    
    
        context.font = '28pt Calibri';
        context.fillStyle = 'black';
        context.beginPath();
        context.arc(x, y, amount, 0, 2 * Math.PI);
        if (amount > 0) {
            setTimeout(function(){
                fadeOut(canvas, message, x, y, --amount);
            }, 2);
        }
        else {
            context.clearRect(0, 0, canvas.width, canvas.height);
        }
    }
    

    在此处查看实际操作:http://jsfiddle.net/2p9dn8ed/42/

    或者在sn-p中:

    var canvas = document.getElementById('myCanvas');
    var context = canvas.getContext('2d');
    var x = 0;
    var y = 0;
    var width = window.innerWidth;
    var height = window.innerHeight;
    var imageObj = new Image();
    console.log(window.innerWidth + "----" + window.innerHeight);
    
    //Create another canvas to darw a resized image to.
    var imageResized = document.createElement('canvas');
    imageResized.width = width;
    imageResized.height = height;
    //Wait for the original image to low to draw the resize.
    imageObj.onload = function() {
        //Find hoe mauch to scale the image up to cover.
        var scaleX = width / imageObj.width;
        var scaleY = height / imageObj.height;
        var scaleMax = Math.max(scaleX, scaleY);
        var ctx = imageResized.getContext('2d');
        ctx.scale(scaleMax, scaleMax);
        ctx.drawImage(imageObj, 0, 0);
    };
    
    imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/darth-vader.jpg';
    
    
    function writeMessage(canvas, message, x, y) {
        var context = canvas.getContext('2d');
        context.clearRect(0, 0, canvas.width, canvas.height);
        
        var pattern = context.createPattern(imageResized, 'no-repeat');//Use imageResized, not imageObj.
        context.fillStyle = pattern;
        context.fill();    
        
        context.font = '28pt Calibri';
        context.fillStyle = 'black';
        //context.fillText(message, x, y);
        context.beginPath();
        context.arc(x, y, 60, 0, 2 * Math.PI);
        //context.stroke();
    	//
    	
    }
    
    function fadeOut(canvas, message, x, y, amount) {    
        var context = canvas.getContext('2d');
        context.clearRect(0, 0, canvas.width, canvas.height);
        
        var pattern = context.createPattern(imageResized, 'no-repeat');//Use imageResized, not imageObj.
        context.fillStyle = pattern;
        context.fill();    
        
        context.font = '28pt Calibri';
        context.fillStyle = 'black';
        //context.fillText(message, x, y);
        context.beginPath();
        context.arc(x, y, amount, 0, 2 * Math.PI);
        //context.stroke();
    	//
        if (amount > 0) {
            setTimeout(function(){
                fadeOut(canvas, message, x, y, --amount);
            }, 2);
        }
        else {
            context.clearRect(0, 0, canvas.width, canvas.height);
        }
    }
    
    function getMousePos(canvas, evt) {
        var rect = canvas.getBoundingClientRect();
        return {
            x: evt.clientX - rect.left,
            y: evt.clientY - rect.top
        };
    }
    
    canvas.addEventListener('mousemove', function (evt) {
        var mousePos = getMousePos(canvas, evt);
        var message = 'Mouse position: ' + mousePos.x + ',' + mousePos.y;
        writeMessage(canvas, message, mousePos.x, mousePos.y);
    
    }, false);
        
        canvas.addEventListener('mouseout', function(evt){
            var mousePos = getMousePos(canvas, evt);
            var message = 'Mouse position: ' + mousePos.x + ',' + mousePos.y;
            console.log(1);
            fadeOut(canvas, message, mousePos.x, mousePos.y, 60);
        });
    
    
    // Get the canvas element form the page
    var canvas = document.getElementById("myCanvas");
     
    /* Rresize the canvas to occupy the full page, 
       by getting the widow width and height and setting it to canvas*/
     
    canvas.width  = window.innerWidth;
    canvas.height = window.innerHeight;
    anvas, img {
        display:block;
        margin:1em auto;
        border:1px solid black;
    }
    canvas {
        background:url('../img/spiral_galaxy-1920x1080.jpg');
        background-size: cover;
    	position: absolute;
    	left: 0; top: 0;
    	width: 100%; height: 100%;
    	z-index:-1;
    }
    div{
       width:200px;
        height:200px;
        background:rgba(0,0,0,0.5);
        position: fixed;
        top: 20%;
        left: 20%;
    }
    <canvas id="myCanvas" width="578" height="400"></canvas>
    <div><h1>TEXT</h1></div>

    【讨论】:

    • 感谢您的解决方案。这似乎是正确的答案。但是,我尝试通过反转代码的某些部分来创建淡入淡出效果。但不起作用。你能在这里检查一下吗?JSFiddle
    • 不客气。我在该代码中进行了一些修改并修复了一些错误,并引入了一个就绪变量,当淡入完成时该变量设置为 true(因此 mousemove 不会立即接管)。给你:jsfiddle.net/2p9dn8ed/44
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 2022-07-06
    • 1970-01-01
    相关资源
    最近更新 更多