uni-canvas-image 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uni-canvas-image",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "租房项目使用uniapp canvas合并图片",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -50,15 +50,16 @@ export async function base64ToBlob(base64Data) {
50
50
  export async function drawImageToCanvas(arr, canvasId = 'canvas-1', api) {
51
51
  return new Promise((resolve, reject) => {
52
52
  const ctx = uni.createCanvasContext(canvasId)
53
- context.setFillStyle('#fff')
54
- context.fillRect(0, 0, 375, 300)
53
+ ctx.setFillStyle('#fff')
54
+ ctx.fillRect(0, 0, 375, 300)
55
55
  arr.forEach((item) => {
56
56
  if (item.type === 'image') {
57
57
  const { path, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight } = item
58
58
  ctx.drawImage(path, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
59
59
  } else if (item.type === 'text') {
60
- const { text, x = 10, y = 20, fontSize = 16, color = '#000' } = item
61
- ctx.setFontSize(fontSize)
60
+ const { text, x = 10, y = 20, fontSize = 16, color = '#000', fontWeight = 'normal' } = item
61
+ ctx.font = `${fontWeight} ${fontSize}px sans-serif`
62
+ // ctx.setFontSize(fontSize)
62
63
  ctx.setFillStyle(color)
63
64
  ctx.setTextBaseline('top')
64
65
  ctx.fillText(text, x, y)
@@ -1,14 +1,12 @@
1
1
  import { directionEnum, getImageInfo, drawImageToCanvas, handleParagraph } from '../common/index'
2
2
  const categoryEnum = [
3
- { text: '单价', x: 10, y: 246, fontSize: 14, color: '#9B9B9B' },
4
- { text: '(元/m²/天)', x: 34, y: 248, fontSize: 10, color: '#9B9B9B' },
5
- { text: '面积', x: 160, y: 246, fontSize: 14, color: '#9B9B9B' },
6
- { text: '(m²)', x: 184, y: 248, fontSize: 10, color: '#9B9B9B' },
7
- { text: '朝向', x: 310, y: 246, fontSize: 14, color: '#9B9B9B' },
8
- { text: '', x: 10, y: 276, fontSize: 10, color: '#FF703B' },
9
- { name: '单价', key: 'rentDayCeilPrice', x: 20, y: 270, fontSize: 18, color: '#FF703B' },
10
- { name: '面积', key: 'houseArea', x: 160, y: 270, fontSize: 18, color: '#FF703B' },
11
- { name: '朝向', key: 'direction', x: 310, y: 270, fontSize: 18, color: '#FF703B' },
3
+ { text: '单价', x: 10, y: 246, fontSize: 18, color: '#9B9B9B' },
4
+ { text: '(/m²/天)', x: 36, y: 248, fontSize: 14, color: '#9B9B9B' },
5
+ { text: '面积', x: 150, y: 246, fontSize: 18, color: '#9B9B9B' },
6
+ { text: '朝向', x: 290, y: 246, fontSize: 18, color: '#9B9B9B' },
7
+ { name: '单价', key: 'rentDayCeilPrice', unit: '元', x: 10, y: 270, fontSize: 24, color: '#ca4e21' },
8
+ { name: '面积', key: 'houseArea', unit: 'm²', x: 150, y: 270, fontSize: 24, color: '#ca4e21' },
9
+ { name: '朝向', key: 'direction', x: 290, y: 270, fontSize: 24, color: '#ca4e21' },
12
10
  ]
13
11
  /**
14
12
  *
@@ -17,7 +15,7 @@ const categoryEnum = [
17
15
  */
18
16
  async function houseImage(params = {}) {
19
17
  const { data, canvasId, api } = params
20
- if (!data) return reject('数据不能为空')
18
+ if (!data) return console.log('数据不能为空')
21
19
  const items = Array.isArray(data) ? data : [data]
22
20
  return items.length > 1
23
21
  ? await handleItems(items, canvasId, api) // 多个房源
@@ -34,18 +32,18 @@ async function handleItem(obj, canvasId, api) {
34
32
  dWidth: 375,
35
33
  dHeight: 170,
36
34
  }
37
- const paragraphInfos = handleParagraph(obj.title)
35
+ const paragraphInfos = handleParagraph(obj.title, 21, 1)
38
36
  const titleInfos = paragraphInfos.map((text, index) => {
39
37
  return {
40
38
  type: 'text',
41
39
  text,
42
40
  x: 10,
43
41
  y: 180 + index * 20,
44
- fontSize: 14,
42
+ fontSize: 18,
45
43
  color: '#000',
46
44
  }
47
45
  })
48
- const tagsInfos = hanleTagsInfo(obj.tags, 14, titleInfos.length > 1 ? 224 : 216)
46
+ const tagsInfos = hanleTagsInfo(obj.tags.slice(0, 3), 14, 212)
49
47
  const categoryInfos = handleCategoryInfo(obj, categoryEnum)
50
48
  const newObj = [newImgInfo, ...titleInfos, ...tagsInfos, ...categoryInfos]
51
49
  const url = await drawImageToCanvas(newObj, canvasId, api)
@@ -56,7 +54,6 @@ function handleItems(arr, canvasId, api) {
56
54
  return new Promise(async (resolve, reject) => {
57
55
  const newObj = []
58
56
  const newArr = arr.slice(0, 2)
59
- console.log('🚀 ~ handleItems ~ newArr:', newArr)
60
57
  for (let i = 0; i < newArr.length; i++) {
61
58
  const obj = newArr[i]
62
59
  // 图片
@@ -76,21 +73,19 @@ function handleItems(arr, canvasId, api) {
76
73
  text,
77
74
  x: 160,
78
75
  y: 10 + i * 152 + index * 20,
79
- fontSize: 14,
76
+ fontSize: 16,
80
77
  color: '#000',
81
78
  }
82
79
  })
83
80
  // 标签
84
- const tagsInfos = hanleTagsInfo(obj.tags, 160, (titleInfos.length > 1 ? 60 : 54) + i * 152)
81
+ const tagsInfos = hanleTagsInfo(obj.tags.slice(0, 2), 160, (titleInfos.length > 1 ? 60 : 54) + i * 152)
85
82
  // 分类信息
86
83
  const category = [
87
- { text: '单价', x: 160, y: 90 + i * 152, fontSize: 14, color: '#9B9B9B' },
88
- { text: '(元/m²/天)', x: 184, y: 92 + i * 152, fontSize: 10, color: '#9B9B9B' },
89
- { text: '面积', x: 290, y: 90 + i * 152, fontSize: 14, color: '#9B9B9B' },
90
- { text: '(m²)', x: 314, y: 92 + i * 152, fontSize: 10, color: '#9B9B9B' },
91
- { text: '', x: 160, y: 124 + i * 152, fontSize: 10, color: '#FF703B' },
92
- { name: '单价', key: 'rentDayCeilPrice', x: 170, y: 118 + i * 152, fontSize: 18, color: '#FF703B' },
93
- { name: '面积', key: 'houseArea', x: 290, y: 118 + i * 152, fontSize: 18, color: '#FF703B' },
84
+ { text: '单价', x: 160, y: 90 + i * 152, fontSize: 18, color: '#9B9B9B' },
85
+ { text: '(/m²/天)', x: 188, y: 92 + i * 152, fontSize: 14, color: '#9B9B9B' },
86
+ { text: '面积', x: 290, y: 90 + i * 152, fontSize: 18, color: '#9B9B9B' },
87
+ { name: '单价', key: 'rentDayCeilPrice', unit: '元', x: 170, y: 118 + i * 152, fontSize: 24, color: '#FF703B' },
88
+ { name: '面积', key: 'houseArea', unit: 'm²', x: 290, y: 118 + i * 152, fontSize: 24, color: '#FF703B' },
94
89
  ]
95
90
  const categoryInfos = handleCategoryInfo(obj, category)
96
91
  newObj.push(newImgInfo, ...titleInfos, ...tagsInfos, ...categoryInfos)
@@ -107,10 +102,10 @@ function hanleTagsInfo(tags, x = 14, y = 224) {
107
102
  type: 'text',
108
103
  text: tag,
109
104
  // 前面标签的长度和间距计算x坐标
110
- x: x + tags.slice(0, index).reduce((acc, cur) => acc + cur.length, 0) * 12 + index * 18, // 根据
105
+ x: x + tags.slice(0, index).reduce((acc, cur) => acc + cur.length, 0) * 16 + index * 18, // 根据
111
106
  y,
112
- fontSize: 12,
113
- color: '#377DF3',
107
+ fontSize: 16,
108
+ color: '#5d687a',
114
109
  }
115
110
  })
116
111
  //tga背景颜色
@@ -119,10 +114,10 @@ function hanleTagsInfo(tags, x = 14, y = 224) {
119
114
  type: 'rect',
120
115
  x: tag.x - 4,
121
116
  y: tag.y - 4,
122
- width: tag.text.length * 12 + 8,
123
- height: 20,
117
+ width: tag.text.length * 16 + 8,
118
+ height: 24,
124
119
  borderRadius: 4,
125
- color: '#ebf1ff',
120
+ color: '#eceff7',
126
121
  }
127
122
  })
128
123
  return [...rects, ...tagsInfos]
@@ -135,6 +130,8 @@ function handleCategoryInfo(obj, categoryEnum) {
135
130
  if (item.key === 'direction') {
136
131
  item.text = directionEnum[obj?.direction[0]] + (obj?.direction.length > 1 ? '...' : '')
137
132
  } else item.text = obj[item.key] || ''
133
+ item.text += item.unit || ''
134
+ item.fontWeight = 'bold'
138
135
  }
139
136
  return item
140
137
  })
@@ -6,7 +6,7 @@ import { getImageInfo, drawImageToCanvas, handleParagraph } from '../common/inde
6
6
  */
7
7
  async function noteImage(params = {}) {
8
8
  const { data, canvasId, api } = params
9
- if (!data) return reject('数据不能为空')
9
+ if (!data) return console.log('数据不能为空')
10
10
  const items = Array.isArray(data) ? data : [data]
11
11
  return items.length > 1
12
12
  ? await handleItems(items, canvasId, api) // 多个房源