index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import _waterMark from './watermark'
  2. import { dataURLtoFile } from './util'
  3. // 标准参数
  4. var canvas
  5. var ctx
  6. const configDefault = {
  7. width: 200,
  8. height: 200
  9. }
  10. const config = {
  11. text: '深圳市金源信通科技有限公司', // 文字
  12. fontFamily: 'microsoft yahei', // 字体
  13. color: '#999', // 颜色
  14. fontSize: 16, // 大小
  15. opacity: 100, // 透明度
  16. bottom: 10, // 下边位置
  17. right: 10, // 右边位置
  18. ratio: 1// 压缩比
  19. }
  20. /**
  21. * 参数 {Object} opt
  22. * @param {String} text 水印文本,默认'广州流辰信息技术有限公司'
  23. * @param {String} font 水印字体,默认'30px 黑体'
  24. * @param {Int} canvasWidth 单个水印容器宽度,默认500
  25. * @param {Int} canvasHeight 单个水印容器高度,默认200
  26. * @param {String} textAlign 水印文本对齐方式,默认'center'
  27. * @param {String} textStyle 水印文本样式,默认'rgba(100,100,100,0.15)'
  28. * @param {Int} degree 水印文本旋转角度,默认 -20
  29. * @param return
  30. **/
  31. export const watermark = function(opt = {}) {
  32. return new _waterMark(opt)
  33. }
  34. // 将base64转换为文件
  35. export function detailImg(file, option = {}) {
  36. return new Promise(function(resolve, reject) {
  37. const { text, fontFamily, color, fontSize, opacity, bottom, right, ratio } = option
  38. initParams()
  39. fileToBase64(file, initImg)
  40. // 参数初始化
  41. function initParams() {
  42. config.text = text || config.text
  43. config.fontFamily = fontFamily || config.fontFamily
  44. config.color = color || config.color
  45. config.fontSize = fontSize || config.fontSize
  46. config.opacity = opacity || config.opacity
  47. config.bottom = bottom || config.bottom
  48. config.right = right || config.right
  49. config.ratio = ratio || config.ratio
  50. }
  51. // 加载图片
  52. function initImg(data) {
  53. const img = new Image()
  54. img.src = data
  55. img.onload = function() {
  56. var width = img.width
  57. var height = img.height
  58. cretedCanvas(width, height)
  59. ctx.drawImage(img, 0, 0, width, height)
  60. setText(width, height)
  61. resolve(dataURLtoFile(document.getElementById('canvas').toDataURL(file.type, config.ratio), file.name))
  62. }
  63. }
  64. // 创建画板
  65. function cretedCanvas(width, height) {
  66. canvas = document.getElementById('canvas')
  67. if (canvas === null) {
  68. canvas = document.createElement('canvas')
  69. canvas.id = 'canvas'
  70. canvas.className = 'ibps-canvas'
  71. document.body.appendChild(canvas)
  72. }
  73. ctx = canvas.getContext('2d')
  74. canvas.width = width
  75. canvas.height = height
  76. }
  77. // 添加水印
  78. function setText(width, height) {
  79. var txt = config.text
  80. var param = calcParam(txt, width, height)
  81. ctx.font = param.fontSize + 'px ' + config.fontFamily
  82. ctx.fillStyle = config.color
  83. ctx.globalAlpha = config.opacity / 100
  84. ctx.fillText(txt, param.x, param.y)
  85. }
  86. // 计算比例
  87. function calcParam(txt, width, height) {
  88. var x, y
  89. // 字体的比例
  90. var calcFontSize = config.fontSize / configDefault.width
  91. var fontSize = calcFontSize * width
  92. if (config.bottom) {
  93. y = configDefault.height - config.bottom
  94. } else {
  95. y = config.top
  96. }
  97. if (config.right) {
  98. x = configDefault.width - config.right
  99. } else {
  100. x = config.left
  101. }
  102. ctx.font = config.fontSize + 'px ' + config.fontFamily
  103. var txtWidth = Number(ctx.measureText(txt).width)
  104. x = x - txtWidth
  105. var calcPosX = x / configDefault.width
  106. var calcPosY = y / configDefault.height
  107. x = calcPosX * width
  108. y = calcPosY * height
  109. return {
  110. x: x,
  111. y: y,
  112. fontSize: fontSize
  113. }
  114. }
  115. // file转base64
  116. function fileToBase64(file, callback) {
  117. var reader = new FileReader()
  118. reader.readAsDataURL(file)
  119. reader.onload = function(e) {
  120. callback(e.target.result)
  121. }
  122. }
  123. })
  124. }