123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <!DOCTYPE html>
- <html lang="zh">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>wvjs</title>
- </head>
- <body>
- <script type="text/javascript" src="./uni.webview.1.5.5.js"></script>
- <script type="text/javascript">
- // 初始化时候抛送
- uni.webView.postMessage({data: {isInitialize: true}});
- /**
- * 响应 web-view png 数据
- * @param {Object} svg svg格式参数,不能是svg图片
- */
- function onPostMessage(type,resourceId,resource) {
- // 转化svg为png时的类型
- if (type == "svg") {
- svgTurnPng(resourceId,resource);
- };
- };
- /**
- * svg转png
- * @param {Object} svg vg格式参数,不能是svg图片
- */
- function svgTurnPng(id,svg) {
- const src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(svg)}`;
- const img = new Image();
- img.src = src;
- img.onload = () => {
- const canvas = document.createElement('canvas');
- canvas.width = img.width;
- canvas.height = img.height;
- const context = canvas.getContext('2d');
- context.drawImage(img, 0, 0);
- uni.webView.postMessage({data: {id: id,result: canvas.toDataURL('image/png')}});
- };
- };
- </script>
- </body>
- </html>
|