<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body onload="pageLoaded();"> <script language="javascript"> function pageLoaded() { var txt = "105.75,29.47 105.74,29.5 105.77,29.53 105.77,29.53 105.83,29.52"; var array = txt.split(" "); //get canvas by id var canvas = document.getElementById('tCanvas'); //get 2d environment var context = canvas.getContext('2d'); //draw polygon context.beginPath(); context.moveTo(105.81, 29.47); for (var a = 0; a < array.length; a++) { context.lineTo(array[a]); //alert(array[a]); }; context.closePath(); context.stroke(); } </script> <canvas width="400" height="400" id="tCanvas" style="border:black 1px solid;"> </canvas> </body> </html>
第22行,你传进去的一个字符串,一个参数,但是这个接口需要的是两个参数,打开控制台就可以看到错误
for (var a = 0; a < array.length; a++) {
var a=array[a].split(',');
context.lineTo(a[0],a[1]);
//alert(array[a]);
};