canavs下png图转jpg后的黑色背景实例页面

回到相关文章 »

效果:

代码:

HTML代码:
<canvas width="165" height="165"></canvas>
                
JS代码:
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');

var img = new Image(), img2 = new Image();
img.onload = function () {
    context.drawImage(this, 0, 0, 165, 165);
    // 转jpg格式
    img2.src = canvas.toDataURL('image/jpeg');
    canvas.parentNode.append(img2);
};
img.src = './riziyan-favicon.png';