今天下午微信朋友圈炸了,全部人都在要国旗。迎国庆换新颜活动 | 腾讯新闻的合成服务 微信中打开。
咱们呢,也来蹭蹭热度,聊聊其中的技术。html
固然,上面只是我觉得。经过观察分析节点,我发现前端
blob:
开头scale、translate
等transfrom
来支持html2canvas
库由于个人公众号是我的号,没权限。因此咱们稍微的改一下。
对不起父老乡亲们,昨天我没写完就下班了。测试地址,终于写完了。下面说说关键点算法
上传图片
使用FileAPI
获取文件,URL.createObjectURL(file)
将file
转换url
canvas
// 上传头像功能块 uploadInput.addEventListener('change', function(){ if(this.files.length){ var file = this.files[0]; avatarView.style.backgroundImage = 'url('+URL.createObjectURL(file)+')'; // avatarView.src=URL.createObjectURL(file) } })
手势操做e.touches
里面会有当前触发点。经过transfrom
来合成,固然由于多个值会形成坐标系偏移,我使用多层来处理。微信
<div class="avatarViewScale" id="avatarViewScale" style="transform: scale(1,1)"> <div class="avatarViewTranslate" id="avatarViewTranslate" style="transform: translate(0,0)"> <div class="avatar avatarView" src="https://www.lilnong.top/static/img/avator/avator-14.jpg" id="avatarView"></div> </div> </div>
touchstart
记录点与touchmove
记录点的插值。比较好算cavnas
的drawImage
来合成。我以为偏移量应该要注意一下。canvas.toDataURL('image/jpeg')
来生成base64url
而后放入img
。经过微信自带的长按下载来实现 太忙了。好不容易写完。感受实现上仍是蛮简单的,作好就不容易了。app
更新时间 2019年9月26日11:34:54
https://www.lilnong.top/stati...测试
增长了旋转功能ui
ctx.translate(widthHeight / 2, widthHeight / 2) ctx.transform.apply(ctx, getComputedStyle(avatarViewRotate).transform.match(/-?[\d.]+/g)) ctx.translate(-1 * widthHeight / 2, -1 * widthHeight / 2);
更新时间 2019年9月26日13:47:19
https://www.lilnong.top/stati...this
更新时间 2019年10月9日09:08:37url