资讯专栏INFORMATION COLUMN

canvas拼图功能实现

entner / 3549人阅读

摘要:绘制图片文字按比例计算不同手机的百分比间距将闭包引用清除,释放内存不支持

最近做项目的时候遇到照片拼图的功能,便在这里分享自己的封装的canvas拼图功能,可能代码写的不好,如果有疑问或者是有更好的方法的,可以私聊我,或者是评论指出,感谢各位

实现的思路其实挺简单的,主要是通过服务端获取图片链接,图片宽度,图片高度,然后利用简单的递归实现就行了(注意移动端需要采用2倍数的比例,否则会出现图片模糊的问题)

/**

</>复制代码

  1. * canvas绘图数据
  2. * @param {Object[]} option.photoData
  3. * @param {string} option.photoData[].photo - 照片的链接地址
  4. * @param {number} option.photoData[].width - 照片的宽度
  5. * @param {number} option.photoData[].height - 照片的高度
  6. * @param {Object[]} option.wordData
  7. * @param {string} option.wordData[].color - 文字的颜色
  8. * @param {number} option.wordData[].fontSize - 文字的大小
  9. * @param {string} option.wordData[].fontWeight - 文字的粗细
  10. * @param {number} option.wordData[].left - 文字的左边距
  11. * @param {number} option.wordData[].top - 文字的上边距
  12. * @param {string} option.wordData[].word - 文字的内容
  13. * @param {Object[]} option.iconData
  14. * @param {string} option.iconData[].photo - icon的链接地址
  15. * @param {number} option.iconData[].left - icon的左边距
  16. * @param {number} option.iconData[].top - icon的上边距
  17. * @param {number} option.iconData[].width - icon的宽度
  18. * @param {number} option.iconData[].height - icon的高度
  19. *
  20. */

function canvasDraw(option){

</>复制代码

  1. var canvas = document.createElement("canvas"),
  2. ctx = canvas.getContext("2d"),
  3. clientWidth = document.documentElement.clientWidth,
  4. canvasHeight = 0,
  5. distance = 0,
  6. photoCount = 0,
  7. iconCount = 0;
  8. // canvas中手机上一倍绘图会模糊,需采用两倍,pc端不会。
  9. clientWidth = clientWidth > 480? 480 * 2 : clientWidth * 2;
  10. option.photoData.forEach(function(item,index,picArr){
  11. if (!index) {
  12. item.distance = 0;
  13. }else if(index){
  14. distance += Math.floor(clientWidth / option.photoData[index - 1].width * option.photoData[index - 1].height)
  15. item.distance = distance;
  16. }
  17. canvasHeight += Math.floor(clientWidth / item.width * item.height);
  18. item.imgHeight = Math.floor(clientWidth / item.width * item.height);
  19. })
  20. console.log(option.photoData)
  21. if (ctx) {
  22. canvas.width = clientWidth;
  23. canvas.height = canvasHeight + clientWidth / 4 * 2
  24. ctx.fillStyle = "#fff"
  25. ctx.fillRect(0, 0, canvas.width, canvas.height)
  26. // 绘制图片文字
  27. if(option.wordData.length){
  28. option.wordData.forEach(function(item,index){
  29. ctx.fillStyle = item.color;
  30. ctx.font = "normal normal " + item.fontWeight + " " + calculate(item.fontSize) + "px Microsoft YaHei";
  31. ctx.textAlign = "left";
  32. ctx.fillText(item.word, calculate(item.left), canvasHeight + calculate(item.top));
  33. })
  34. }
  35. //按比例计算不同手机的百分比间距
  36. function calculate(num){
  37. return Math.floor(clientWidth * num / 750)
  38. }
  39. drawPhoto("photo0")
  40. function drawPhoto(photoDom){
  41. var photoDom = new Image();
  42. photoDom.setAttribute("crossOrigin", "Anonymous");
  43. photoDom.src = option.photoData[photoCount].photo;
  44. photoDom.onload = function(){
  45. ctx.drawImage(photoDom, 0, option.photoData[photoCount].distance, clientWidth, option.photoData[photoCount].imgHeight);
  46. photoCount++;
  47. if (photoCount == option.photoData.length) {
  48. drawIcon("icon0")
  49. function drawIcon(iconDom){
  50. var iconDom = new Image();
  51. iconDom.setAttribute("crossOrigin", "Anonymous");
  52. iconDom.src = option.iconData[iconCount].icon;
  53. iconDom.onload = function(){
  54. ctx.drawImage(iconDom, calculate(option.iconData[iconCount].left), canvasHeight + calculate(option.iconData[iconCount].top), calculate(option.iconData[iconCount].width), calculate(option.iconData[iconCount].height))
  55. iconCount++;
  56. if (iconCount == option.iconData.length) {
  57. var imageURL = canvas.toDataURL("image/jpeg")
  58. document.getElementsByClassName("shareImg")[0].setAttribute("src", imageURL)
  59. //将闭包引用清除,释放内存;
  60. drawPhoto = null;
  61. }else{
  62. drawIcon("icon" + iconCount)
  63. }
  64. }
  65. }
  66. }else{
  67. drawPhoto("photo"+ photoCount)
  68. }
  69. }
  70. }
  71. }else{
  72. console.log("不支持canvas")
  73. }
  74. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/108710.html

相关文章

  • canvas拼图功能实现

    摘要:绘制图片文字按比例计算不同手机的百分比间距将闭包引用清除,释放内存不支持 最近做项目的时候遇到照片拼图的功能,便在这里分享自己的封装的canvas拼图功能,可能代码写的不好,如果有疑问或者是有更好的方法的,可以私聊我,或者是评论指出,感谢各位 实现的思路其实挺简单的,主要是通过服务端获取图片链接,图片宽度,图片高度,然后利用简单的递归实现就行了(注意移动端需要采用2倍数的比例,否则会...

    付伦 评论0 收藏0
  • canvas拼图功能实现

    摘要:绘制图片文字按比例计算不同手机的百分比间距将闭包引用清除,释放内存不支持 最近做项目的时候遇到照片拼图的功能,便在这里分享自己的封装的canvas拼图功能,可能代码写的不好,如果有疑问或者是有更好的方法的,可以私聊我,或者是评论指出,感谢各位 实现的思路其实挺简单的,主要是通过服务端获取图片链接,图片宽度,图片高度,然后利用简单的递归实现就行了(注意移动端需要采用2倍数的比例,否则会...

    Kyxy 评论0 收藏0
  • 小程序—九宫格心形拼图

    摘要:而微信小程序中也刚好有进度条这个组件。推荐和意见反馈推荐给朋友意见反馈这个两个功能就是用了,微信小程序的组件,这里需要注意的就是,在清除的默认样式时,需要把的伪元素的边框也去掉。总结这次做的这个九宫格心形拼图的小程序,第一版已经上线了。 说明 前几天在朋友圈看到好几次这种图片。 showImg(https://segmentfault.com/img/bVbeAoX?w=321&h=3...

    myeveryheart 评论0 收藏0
  • canvas自由拼图

    摘要:自由拼图自由拼图是美图秀秀中的一个功能,它可以让用户在背景图片上插入自己的图片,并可以对插入图片旋转,拖拽,缩放。效果本屌之前用实现过,参见仿美图秀秀的自由拼图注意里面基本上没代码说明这里用的实现。 自由拼图? 自由拼图是美图秀秀中的一个功能,它可以让用户在背景图片上插入自己的图片,并可以对插入图片旋转,拖拽,缩放。当然,如果用户对插入的图片不满意,可以用另一张图片替换选中的图片,或者...

    megatron 评论0 收藏0

发表评论

0条评论

entner

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<