多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# .toTempFilePath ### string Canvas.toTempFilePath(Object object) 将当前 Canvas 保存为一个临时文件,并生成相应的临时文件路径。 #### 参数 ##### Object object 属性类型默认值是否必填说明支持版本xnumber0否截取 canvas 的左上角横坐标ynumber0否截取 canvas 的左上角纵坐标widthnumbercanvas 的宽度否截取 canvas 的宽度heightnumbercanvas 的高度否截取 canvas 的高度destWidthnumbercanvas 的宽度否目标文件的宽度,会将截取的部分拉伸或压缩至该数值destHeightnumbercanvas 的高度否目标文件的高度,会将截取的部分拉伸或压缩至该数值fileTypestringpng否目标文件的类型qualitynumber1.0否jpg图片的质量,仅当 fileType 为 jpg 时有效。取值范围为 0.0(最低)- 1.0(最高),不含 0。不在范围内时当作 1.0successfunction否接口调用成功的回调函数failfunction否接口调用失败的回调函数completefunction否接口调用结束的回调函数(调用成功、失败都会执行)**object.fileType 的合法值** 值说明jpgjpg 文件pngpng 文件#### 返回值 ##### string canvas 生成的临时文件路径 #### 示例代码 Canvas.toTempFilePath ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">canvas</span><span class="pun">.</span><span class="pln">toTempFilePath</span><span class="pun">({</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> x</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> y</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">150</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> destWidth</span><span class="pun">:</span><span class="pln"> </span><span class="lit">400</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> destHeight</span><span class="pun">:</span><span class="pln"> </span><span class="lit">300</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> success</span><span class="pun">:</span><span class="pln"> </span><span class="pun">(</span><span class="pln">res</span><span class="pun">)</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="pun">></span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> wx</span><span class="pun">.</span><span class="pln">shareAppMessage</span><span class="pun">({</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> imageUrl</span><span class="pun">:</span><span class="pln"> res</span><span class="pun">.</span><span class="pln">tempFilePath</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">})</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> </ol> ``` Canvas.toTempFilePathSync ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">let tempFilePath </span><span class="pun">=</span><span class="pln"> canvas</span><span class="pun">.</span><span class="pln">toTempFilePathSync</span><span class="pun">({</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> x</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> y</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> width</span><span class="pun">:</span><span class="pln"> </span><span class="lit">200</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> height</span><span class="pun">:</span><span class="pln"> </span><span class="lit">150</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> destWidth</span><span class="pun">:</span><span class="pln"> </span><span class="lit">400</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> destHeight</span><span class="pun">:</span><span class="pln"> </span><span class="lit">300</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">wx</span><span class="pun">.</span><span class="pln">shareAppMessage</span><span class="pun">({</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> imageUrl</span><span class="pun">:</span><span class="pln"> tempFilePath</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> </ol> ``` > 原文: <https://developers.weixin.qq.com/minigame/dev/api/render/canvas/Canvas.toTempFilePath.html>