html2canvas页面生成base64,springboot后台转成图片

本文介绍了如何使用html2canvas将页面div内容转换为Base64字符串,并在SpringBoot后端将其转换为图片。建议在div中使用table进行内容布局以避免变形。
摘要由CSDN通过智能技术生成
function createImage(name){
    $("#createImage").removeClass("current_s");
    var baseDiv = document.querySelector('#baseDiv');
    var width = baseDiv.offsetWidth;//div宽
    var height = baseDiv.offsetHeight;//div高
    var canvas = document.createElement("canvas");
    var content = canvas.getContext("2d");
    var scale = 4.1;//放大倍数

    canvas.width = width * scale;//canvas宽度
    canvas.height = height * scale;//canvas高度

    content.scale(scale,scale);
    var rect = baseDiv.getBoundingClientRect();//获取元素相对于视察的偏移量
    content.translate(-rect.left,-rect.top);//设置context位置,值为相对于视窗的偏移量负值,让图片复位

    var opts = {
        dpi: window.devicePixelRatio*4.1,
        scale: scale,
        canvas: canvas,
        logging: false,//是否输出日志
        width: width,
        height: height
    };
    html2canvas(baseDiv,opts).then(function (canvas) {
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值