html canvas 左右控制,html2canvas - 如何定义顶部,左侧,底部,右侧有自动裁剪?...

你可以简单地使用屏幕外的画布上,您将吸引你的渲染画布所需的偏移量。

这是一个快速书写的功能,可能无法满足所有要求,但至少可以给你一个想法: 请注意,它使用latest html2canvas version (0.5.0-beta4),它现在返回一个Promise。

function screenshot(element, options = {}) {

// our cropping context

let cropper = document.createElement('canvas').getContext('2d');

// save the passed width and height

let finalWidth = options.width || window.innerWidth;

let finalHeight = options.height || window.innerHeight;

// update the options value so we can pass it to h2c

if (options.x) {

options.width = finalWidth + options.x;

}

if (options.y) {

options.height = finalHeight + options.y;

}

// chain h2c Promise

return html2canvas(element, options).then(c => {

// do our cropping

cropper.canvas.width = finalWidth;

cropper.canvas.height = finalHeight;

cropper.drawImage(c, -(+options.x || 0), -(+options.y || 0));

// return our canvas

return cropper.canvas;

});

}

而且由于stacksnippets®使用了一些强大的安全性在他们的框架调用它像

screenshot(yourElement, {

x: 20, // this are our custom x y properties

y: 20,

width: 150, // final width and height

height: 150,

useCORS: true // you can still pass default html2canvas options

}).then(canvas => {

//do whatever with the canvas

})

,我们不能让一个现场演示在这里,但你可以找到一个在这

哦,对于那些想要ES5版本支持旧的html2canvas版本的人,你只需要在回调的回调中包装裁剪功能,或者对于懒惰的回调,这里是a fiddle。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值