tn-waterMark.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <template>
  2. <view class="content">
  3. <view v-for="item in canvasArr">
  4. <canvas canvas-id="myCanvas" :style="{width:canvasWidth+'px',height:canvasHeight+'px'}"></canvas>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. props: {
  11. },
  12. data() {
  13. return {
  14. canvasArr: [],
  15. platformType: 'other',
  16. canvasWidth: '',
  17. canvasHeight: '',
  18. pixelRatio: 1
  19. };
  20. },
  21. mounted() {
  22. let _this = this;
  23. // #ifdef H5
  24. _this.platformType = 'h5';
  25. // #endif
  26. wx.getSystemInfo({
  27. success: function(data) {
  28. console.log(data);
  29. _this.pixelRatio = data.pixelRatio;
  30. }
  31. });
  32. },
  33. methods: {
  34. addWaterMark(options) {
  35. let _this = this;
  36. if (_this.platformType == 'h5') {
  37. _this.h5CanvasDraw(options);
  38. } else {
  39. _this.otherCanvasReady(options);
  40. }
  41. },
  42. otherCanvasReady(options) {
  43. let _this = this;
  44. uni.getImageInfo({
  45. src: options.url,
  46. success: function(img) {
  47. let size = options.maxSize || 0;
  48. if (options.maxSize) {
  49. if (img.width > img.height) {
  50. if (img.width < size) {
  51. size = img.width / _this.pixelRatio;
  52. } else {
  53. size = size / _this.pixelRatio
  54. }
  55. _this.canvasWidth = size;
  56. _this.canvasHeight = img.height / (img.width / size);
  57. } else {
  58. if (img.height < size) {
  59. size = img.height / _this.pixelRatio;
  60. } else {
  61. size = size / _this.pixelRatio
  62. }
  63. _this.canvasHeight = size;
  64. _this.canvasWidth = img.width / (img.height / size);
  65. }
  66. } else {
  67. _this.canvasWidth = img.width / _this.pixelRatio
  68. _this.canvasHeight = img.height / _this.pixelRatio
  69. }
  70. _this.canvasArr.splice(0, 1, 1);
  71. _this.$nextTick(function() {
  72. _this.otherCanvasDraw(options, img);
  73. });
  74. }
  75. });
  76. },
  77. otherCanvasDraw(options, img) {
  78. let _this = this;
  79. const ctx = uni.createCanvasContext('myCanvas', _this)
  80. ctx.drawImage(img.path, 0, 0, _this.canvasWidth, _this.canvasHeight)
  81. ctx.textAlign = options.textAlign || 'top'
  82. ctx.textBaseline = options.textBaseline || 'top'
  83. let imageArr = [];
  84. let textArr = [];
  85. options.watermark.some(function(item, index) {
  86. if (item.type == 'image') {
  87. imageArr.push(item);
  88. }
  89. if (item.type == 'text') {
  90. textArr.push(item);
  91. }
  92. })
  93. textArr.some(function(item, index) {
  94. ctx.font = (item.font || '16px') + ' Arial'
  95. ctx.shadowColor = item.shadowColor || '#000'
  96. ctx.shadowOffsetX = item.shadowOffsetX || 0;
  97. ctx.shadowOffsetY = item.shadowOffsetY || 0;
  98. ctx.fillStyle = item.fillStyle || '#fff'
  99. const fontWidth = ctx.measureText(item.content).width / 2; //获取文本宽度
  100. let rotate = item.rotate || 0
  101. item.x = item.x || 0
  102. item.y = item.y || 0
  103. if (item.isRepeat) {
  104. item.repeatWidth = item.repeatWidth || _this.canvasWidth
  105. item.repeatHeight = item.repeatHeight || _this.canvasHeight
  106. item.distanceX = item.distanceX || 60
  107. item.distanceY = item.distanceY || 50
  108. ctx.rotate(-rotate * Math.PI / 180)
  109. for (let i = item.x; i < item.repeatWidth; i += item.distanceX) {
  110. for (let j = item.y; j < item.repeatHeight; j += item.distanceY) {
  111. // 填充文字,x 间距, y 间距
  112. console.log(item.x, item.y);
  113. ctx.fillText(item.content, i, j)
  114. }
  115. }
  116. ctx.rotate(rotate * Math.PI / 180);
  117. } else {
  118. ctx.rotate(-rotate * Math.PI / 180)
  119. ctx.fillText(item.content, item.x, item.y);
  120. ctx.rotate(rotate * Math.PI / 180);
  121. }
  122. })
  123. imageArr.some(function(item, index) {
  124. uni.getImageInfo({
  125. src: item.path,
  126. success: function(reimg) {
  127. let rotate = item.rotate || 0
  128. item.x = item.x || 0
  129. item.y = item.y || 0
  130. item.width = item.width || reimg.width / _this.pixelRatio
  131. item.height = item.height || reimg.height / _this.pixelRatio
  132. if (item.isRepeat) {
  133. item.repeatWidth = item.repeatWidth || _this.canvasWidth
  134. item.repeatHeight = item.repeatHeight || _this.canvasHeight
  135. item.distanceX = item.distanceX || 60
  136. item.distanceY = item.distanceY || 50
  137. ctx.rotate(-rotate * Math.PI / 180);
  138. for (let i = item.x; i < item.repeatWidth; i += item.distanceX) {
  139. for (let j = item.y; j < item.repeatHeight; j += item.distanceY) {
  140. // 填充文字,x 间距, y 间距
  141. console.log(item.x, item.y);
  142. ctx.drawImage(reimg.path, i, j, item.width, item.height)
  143. }
  144. }
  145. ctx.rotate(rotate * Math.PI / 180);
  146. } else {
  147. ctx.rotate(-rotate * Math.PI / 180)
  148. ctx.drawImage(reimg.path, item.x, item.y, item.width, item.height)
  149. ctx.rotate(rotate * Math.PI / 180)
  150. }
  151. if (imageArr.length == index + 1) {
  152. _this.otherGenerateBase64(options, ctx);
  153. }
  154. }
  155. })
  156. })
  157. },
  158. otherGenerateBase64(options, ctx) {
  159. let _this = this;
  160. ctx.draw(true, function(ret) {
  161. uni.canvasToTempFilePath({
  162. canvasId: 'myCanvas', // canvas id
  163. quality: options.quality || 1,
  164. complete(res) {
  165. const savedFilePath = res.tempFilePath //相对路径
  166. const path = plus.io.convertLocalFileSystemURL(savedFilePath) //绝对路径
  167. const fileReader = new plus.io.FileReader()
  168. fileReader.readAsDataURL(path)
  169. fileReader.onloadend = (res) => { //读取文件成功完成的回调函数
  170. _this.$emit("pBackImage", res.target.result);
  171. }
  172. }
  173. })
  174. })
  175. },
  176. h5CanvasDraw(options) {
  177. let _this = this;
  178. const img = new Image()
  179. img.src = options.url
  180. // img.crossOrigin = 'anonymous'
  181. img.onload = function() {
  182. const canvas = document.createElement('canvas')
  183. let size = options.maxSize || 0;
  184. if (options.maxSize) {
  185. if (img.width > img.height) {
  186. if (img.width < size) {
  187. size = img.width;
  188. }
  189. _this.canvasWidth = size;
  190. _this.canvasHeight = img.height / (img.width / size);
  191. } else {
  192. if (img.height < size) {
  193. size = img.height;
  194. }
  195. _this.canvasHeight = size;
  196. _this.canvasWidth = img.width / (img.height / size);
  197. }
  198. } else {
  199. _this.canvasWidth = img.width
  200. _this.canvasHeight = img.height
  201. }
  202. canvas.width = _this.canvasWidth
  203. canvas.height = _this.canvasHeight
  204. const ctx = canvas.getContext('2d')
  205. ctx.drawImage(img, 0, 0)
  206. ctx.textAlign = options.textAlign || 'top'
  207. ctx.textBaseline = options.textBaseline || 'top'
  208. let imageArr = [];
  209. let textArr = [];
  210. options.watermark.some(function(item, index) {
  211. if (item.type == 'image') {
  212. imageArr.push(item);
  213. }
  214. if (item.type == 'text') {
  215. textArr.push(item);
  216. }
  217. })
  218. textArr.some(function(item, index) {
  219. ctx.font = (item.font || '16px') + ' Arial'
  220. ctx.shadowColor = item.shadowColor || '#000'
  221. ctx.shadowOffsetX = item.shadowOffsetX || 0;
  222. ctx.shadowOffsetY = item.shadowOffsetY || 0;
  223. ctx.fillStyle = item.fillStyle || '#fff'
  224. const fontWidth = ctx.measureText(item.content).width / 2; //获取文本宽度
  225. let rotate = item.rotate || 0
  226. item.x = item.x || 0
  227. item.y = item.y || 0
  228. if (item.isRepeat) {
  229. item.repeatWidth = item.repeatWidth || _this.canvasWidth
  230. item.repeatHeight = item.repeatHeight || _this.canvasHeight
  231. item.distanceX = item.distanceX || 60
  232. item.distanceY = item.distanceY || 50
  233. ctx.rotate(-rotate * Math.PI / 180)
  234. for (let i = item.x; i < item.repeatWidth; i += item.distanceX) {
  235. for (let j = item.y; j < item.repeatHeight; j += item.distanceY) {
  236. // 填充文字,x 间距, y 间距
  237. console.log(item.x, item.y);
  238. ctx.fillText(item.content, i, j)
  239. }
  240. }
  241. ctx.rotate(rotate * Math.PI / 180);
  242. } else {
  243. ctx.rotate(-rotate * Math.PI / 180)
  244. ctx.fillText(item.content, item.x, item.y);
  245. ctx.rotate(rotate * Math.PI / 180);
  246. }
  247. })
  248. imageArr.some(function(item, index) {
  249. const reimg = new Image()
  250. reimg.src = item.path
  251. reimg.crossOrigin = 'anonymous'
  252. reimg.setAttribute('crossOrigin', 'anonymous');
  253. getImageInfo
  254. reimg.onload = function() {
  255. let rotate = item.rotate || 0
  256. item.x = item.x || 0
  257. item.y = item.y || 0
  258. item.width = item.width || reimg.width
  259. item.height = item.height || reimg.height
  260. if (item.isRepeat) {
  261. item.repeatWidth = item.repeatWidth || _this.canvasWidth
  262. item.repeatHeight = item.repeatHeight || _this.canvasHeight
  263. item.distanceX = item.distanceX || 60
  264. item.distanceY = item.distanceY || 50
  265. ctx.rotate(-rotate * Math.PI / 180);
  266. for (let i = item.x; i < item.repeatWidth; i += item.distanceX) {
  267. for (let j = item.y; j < item.repeatHeight; j += item.distanceY) {
  268. // 填充文字,x 间距, y 间距
  269. console.log(item.x, item.y);
  270. ctx.drawImage(reimg, i, j, item.width, item.height)
  271. }
  272. }
  273. ctx.rotate(rotate * Math.PI / 180);
  274. } else {
  275. ctx.rotate(-rotate * Math.PI / 180)
  276. ctx.drawImage(reimg, item.x, item.y, item.width, item.height)
  277. ctx.rotate(rotate * Math.PI / 180)
  278. }
  279. if (imageArr.length == index + 1) {
  280. const base64Url = canvas.toDataURL()
  281. _this.$emit("pBackImage", base64Url);
  282. }
  283. }
  284. })
  285. }
  286. }
  287. }
  288. }
  289. </script>
  290. <style lang="scss" scoped>
  291. .content {
  292. width: 0;
  293. height: 0;
  294. overflow: hidden;
  295. }
  296. </style>