Canvas文字的渲染--基础
基础使用
context.font = "bold 40px Arial"
context.fillStyle = "#058"
context.fillText( string, x, y, [maxlen] ) // 实心文字
context.strokeText( string, x, y [maxlen] ) // 描边文字
进阶使用
-
渐变
var lenerGrad = context.createLinearGradient(0, 0, 800, 0); linearGrad.addColorStop(0.0, 'red'); linearGrad.addColorStop(0.5, 'yellow'); linearGrad.addColorStop(1.0, 'blue'); context.fillStyle = linearGrad; context.fillText("hello world", 40, 500 );
-
图片背景
var backgroundImg = new Image(); backgroundImg.src = 'xxx.jpg'; backgroundImg.onload = function() { var pattern = context.createPattern( backgroundImg, 'repeat'); context.fillStyle = pattern; context.font = "bold 100px Arial"; context.fillText("hello world", 40, 650); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步