【问题标题】:canvas.toDataURL() creates a png but I want that png to have a certain dimensionscanvas.toDataURL() 创建一个 png 但我希望该 png 具有一定的尺寸
【发布时间】:2016-12-29 13:52:32
【问题描述】:

基本上,我创建的 png 始终具有 300 h 150 的尺寸,我想要更大的尺寸。这是我尝试做的。

  // this was my last trial I tried canging the width in the canvas tag
var newCanvas = $("<canvas/>", {"class" : "pic", "width" : "1000px", "height" :"300px"}).width(1000).height(500)
//  $(".mainContent").prepend(newCanvas);
// var canvasData = dataGatherer();
var canvas = newCanvas[0];
console.log(canvas)
var ctx = canvas.getContext("2d");
ctx.font = "30px Arial"
console.log("datum---TEST--",  datum)
ctx.fillText(datum.reviewText, 10, 50)
console.log(canvas.toDataURL())
datum.img = canvas.toDataURL();

服务器中的某个地方:

   if(req.body.img){
        var img = req.body.img;
        data = img.replace(/^data:image\/\w+;base64,/, "");
        var buf = new Buffer(data, "base64");
        fs.unlink(__dirname + "/../public/another.png", function(){
            fs.writeFile(__dirname + "/../public/another.png", buf, {flag : "w"},function(){
                console.log("++++writen to file+++++")
            })
        });

    }

【问题讨论】:

    标签: html express canvas png


    【解决方案1】:

    如果您希望调整结果 .toDataURL 的大小,则必须调整画布 元素 的大小。

    var canvas= document.createElement('canvas');
    
    // resize the canvas element -- not it's CSS size
    canvas.width=1000;
    canvas.height=300;
    
    ...
    
    // this dataURL will be 1000 x 300
    var dataURL = canvas.toDataURL();
    

    【讨论】:

    • 我刚刚找到了一个答案.. 这看起来可行var newCanvas = $("&lt;canvas class ='pic'&gt;&lt;/canvas&gt;").attr("height", 400).attr("width", 1000)
    • @jackblank ... 轻笑!我就是这么回答的! :-))
    猜你喜欢
    • 1970-01-01
    • 2013-12-23
    • 2021-08-26
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2020-11-29
    • 2018-06-15
    相关资源
    最近更新 更多