uni-canvas-image 1.1.0 → 1.1.1
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 +1 -1
- package/src/common/index.js +9 -4
- package/src/index.js +7 -0
- package/src/util/attractImage.js +34 -0
package/package.json
CHANGED
package/src/common/index.js
CHANGED
|
@@ -57,14 +57,13 @@ export async function drawImageToCanvas(arr, canvasId = 'canvas-1', api) {
|
|
|
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', fontWeight = 'normal' } = item
|
|
61
|
-
ctx.font = `${fontWeight} ${fontSize}px sans-serif`
|
|
62
|
-
// ctx.setFontSize(fontSize)
|
|
60
|
+
const { text, x = 10, y = 20, fontSize = 16, color = '#000', fontWeight = 'normal', fontFamily = 'Regular' } = item
|
|
63
61
|
ctx.setFillStyle(color)
|
|
64
62
|
ctx.setTextBaseline('top')
|
|
63
|
+
ctx.font = `${fontWeight} ${fontSize}px ${fontFamily}`
|
|
65
64
|
ctx.fillText(text, x, y)
|
|
66
65
|
} else if (item.type === 'rect') {
|
|
67
|
-
const { x, y, width, height, borderRadius, color } = item
|
|
66
|
+
const { x, y, width, height, borderRadius = 0, color } = item
|
|
68
67
|
ctx.fillStyle = color // 填充
|
|
69
68
|
roundedRectPath(ctx, x, y, width, height, borderRadius) // x, y, width, height, radius
|
|
70
69
|
ctx.fill()
|
|
@@ -72,6 +71,12 @@ export async function drawImageToCanvas(arr, canvasId = 'canvas-1', api) {
|
|
|
72
71
|
const { dx: x, dy: y, dWidth: width, dHeight: height, color } = item
|
|
73
72
|
ctx.setFillStyle(color) // 填充
|
|
74
73
|
ctx.fillRect(x, y, width, height) // x, y
|
|
74
|
+
} else if (item.type === 'rect3') {
|
|
75
|
+
const gradient = ctx.createLinearGradient(16, 280, 16, 240)
|
|
76
|
+
gradient.addColorStop(0, 'rgba(0, 0, 0, 0.7)')
|
|
77
|
+
gradient.addColorStop(1, 'rgba(0, 0, 0, 0.1)')
|
|
78
|
+
ctx.fillStyle = gradient
|
|
79
|
+
ctx.fillRect(16, 240, 210, 40)
|
|
75
80
|
}
|
|
76
81
|
})
|
|
77
82
|
ctx.draw(false, () => {
|
package/src/index.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
// 房源分享卡片
|
|
1
2
|
export { default as houseImage } from './util/houseImage'
|
|
3
|
+
// 笔记分享卡片
|
|
2
4
|
export { default as noteImage } from './util/noteImage'
|
|
5
|
+
// 多宫格分享卡片
|
|
3
6
|
export { default as coverImage } from './util/coverImage'
|
|
7
|
+
// 招商分享卡片
|
|
8
|
+
export { default as attractImage } from './util/attractImage'
|
|
9
|
+
// 导出通用方法
|
|
10
|
+
export * from './common/index'
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getImageInfo, drawImageToCanvas } from '../common/index'
|
|
2
|
+
|
|
3
|
+
export async function attractImage(params) {
|
|
4
|
+
return new Promise(async (resolve, reject) => {
|
|
5
|
+
const { data, canvasId, api } = params
|
|
6
|
+
const staticInfo = [
|
|
7
|
+
{ type: 'rect', x: 16, y: 34, width: 320, height: 16, color: '#00ffb7' },
|
|
8
|
+
{ type: 'text', text: '主要区域:', x: 16, y: 29, color: '#000', fontSize: 16 },
|
|
9
|
+
{ type: 'text', text: data.buildingCount, x: 340 - String(data.buildingCount).length * 24, y: 80, color: '#FF703B', fontSize: 40, fontWeight: 700 },
|
|
10
|
+
{ type: 'text', text: '楼盘数量', x: 280, y: 130, color: '#000', fontSize: 16 },
|
|
11
|
+
{ type: 'text', text: data.houseCount, x: 340 - String(data.houseCount).length * 24, y: 196, color: '#FF703B', fontSize: 40, fontWeight: 700 },
|
|
12
|
+
{ type: 'text', text: '房源数量', x: 280, y: 246, color: '#000', fontSize: 16 },
|
|
13
|
+
]
|
|
14
|
+
// 区域信息
|
|
15
|
+
const areaInfo = data.mainArea.length > 3 ? data.mainArea.slice(0, 3).join('、') + '...' : data.mainArea.join('、')
|
|
16
|
+
const newAreaInfo = { type: 'text', text: areaInfo, x: 92, y: 24, color: '#000', fontSize: 22, fontWeight: 600 }
|
|
17
|
+
// 图片信息
|
|
18
|
+
const imgInfo = await getImageInfo(data.buildingUrl, 230 / 210)
|
|
19
|
+
const newImgInfo = {
|
|
20
|
+
...imgInfo,
|
|
21
|
+
dx: 16,
|
|
22
|
+
dy: 70,
|
|
23
|
+
dWidth: 230,
|
|
24
|
+
dHeight: 210,
|
|
25
|
+
}
|
|
26
|
+
const maskInfo = { type: 'rect3' }
|
|
27
|
+
const buildingLabel = { type: 'text', text: '主营楼盘:', x: 22, y: 260, color: '#fff', fontSize: 12 }
|
|
28
|
+
data.mainBuilding = data.mainBuilding.join('、').length > 11 ? data.mainBuilding.join('、').slice(0, 11) + '...' : data.mainBuilding.join('、')
|
|
29
|
+
const newBuildingInfo = { type: 'text', text: data.mainBuilding, x: 80, y: 258, color: '#fff', fontSize: 14 }
|
|
30
|
+
const newObj = [...staticInfo, newAreaInfo, newImgInfo, maskInfo, buildingLabel, newBuildingInfo]
|
|
31
|
+
const url = await drawImageToCanvas(newObj, canvasId, api)
|
|
32
|
+
resolve(url)
|
|
33
|
+
})
|
|
34
|
+
}
|