提交 26f3c8a1 编写于 作者: X xty

canvas自定义字体示例

上级 062a2bc8
...@@ -33,7 +33,8 @@ ...@@ -33,7 +33,8 @@
canvasContext: null as CanvasContext | null, canvasContext: null as CanvasContext | null,
renderingContext: null as CanvasRenderingContext2D | null, renderingContext: null as CanvasRenderingContext2D | null,
canvasWidth: 0, canvasWidth: 0,
canvasHeight: 0, canvasHeight: 0,
fontLoaded: false,
names: [...API_PATH, ...API_DRAW, ...API_STATE, ...API_PROPERTIES, "measureText", "path2D"].sort() as string[] names: [...API_PATH, ...API_DRAW, ...API_STATE, ...API_PROPERTIES, "measureText", "path2D"].sort() as string[]
} }
}, },
...@@ -808,34 +809,42 @@ ...@@ -808,34 +809,42 @@
path2DRect.rect(10, 10, 100, 20); path2DRect.rect(10, 10, 100, 20);
context.fill(path2DRect); context.fill(path2DRect);
}, },
fontTTF() { fontTTF() {
const context = this.renderingContext! const context = this.renderingContext!
const fontMap = new Map<string,UTSJSONObject>([ const fontMap = new Map<string, UTSJSONObject>([
["AlimamaDaoLiTiOTF", {path: "/static/font/AlimamaDaoLiTi.otf", text: "阿里妈妈刀隶体字体otf加载成功"} as UTSJSONObject], ["AlimamaDaoLiTiOTF", { path: "/static/font/AlimamaDaoLiTi.otf", text: "阿里妈妈刀隶体字体otf加载成功" } as UTSJSONObject],
["AlimamaDaoLiTiWOFF", {path: "/static/font/AlimamaDaoLiTi.woff", text: "阿里妈妈刀隶体字体Woff加载成功"} as UTSJSONObject], ["AlimamaDaoLiTiWOFF", { path: "/static/font/AlimamaDaoLiTi.woff", text: "阿里妈妈刀隶体字体Woff加载成功" } as UTSJSONObject],
["iconfont-star", {path: "/static/fonts/icon-star.ttf", text: "\ue879"} as UTSJSONObject ], ["iconfont-star", { path: "/static/fonts/icon-star.ttf", text: "\ue879" } as UTSJSONObject],
]); ]);
var yOffset = 0 var yOffset = 0
fontMap.forEach((value: UTSJSONObject, key: string)=>{
console.log(`key:${key} value:${value["path"]}`) var drawFont = (key : string, value : UTSJSONObject) => {
uni.loadFontFace({ console.log(`${key}字体加载成功`)
family: key, context.save()
source: `url('${value["path"]}')`, const text = value["text"] as string
success: () => { context.font = `20px ${key}`
console.log(`${key}字体加载成功`) context.fillStyle = "red"
context.save() yOffset += 25
const text = value["text"] as string context.fillText(text, 0, yOffset)
context.font = `20px ${key}` yOffset += 25
context.fillStyle = "red" context.strokeText(text, 0, yOffset)
yOffset += 25 context.restore()
context.fillText(text, 0, yOffset) }
yOffset += 25 fontMap.forEach((value : UTSJSONObject, key : string) => {
context.strokeText(text, 0, yOffset) console.log(`key:${key} value:${value["path"]}`)
context.restore() if (this.fontLoaded) {
} drawFont(key, value)
}) } else {
}) uni.loadFontFace({
family: key,
source: `url('${value["path"]}')`,
success: () => {
drawFont(key, value)
}
})
}
})
this.fontLoaded = true
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册