mapTexture.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import THREE from 'THREE';
  2. import d3 from 'd3';
  3. var projection = d3.geo.equirectangular()
  4. .translate([1024, 512])
  5. .scale(325);
  6. /* var projection = d3.geo.equirectangular()
  7. .translate([512, 256])
  8. .scale(162.5); 当canvas为1024*512时 */
  9. var _fixType=function(type) {
  10. type = type.toLowerCase().replace(/jpg/i, 'jpeg');
  11. var r = type.match(/png|jpeg|bmp|gif/)[0];
  12. return 'image/' + r;
  13. };
  14. var saveFile=function(data, filename){
  15. var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
  16. save_link.href = data;
  17. save_link.download = filename;
  18. var event = document.createEvent('MouseEvents');
  19. event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
  20. save_link.dispatchEvent(event);
  21. };
  22. var canvasToImg = function(canvas,download){
  23. // 图片导出为 png 格式
  24. var type = 'png';
  25. var imgData = canvas.toDataURL(type);
  26. // 加工image data,替换mime type
  27. if(download){
  28. imgData = imgData.replace(_fixType(type),'image/octet-stream');
  29. // 下载后的图片名
  30. var filename = '4dage_' + (new Date()).getTime() + '.' + type;
  31. // download
  32. saveFile(imgData,filename);
  33. }
  34. return imgData;
  35. }
  36. export function mapTexture(geojson, color, lineWidth, lineColor) {
  37. var texture, context, canvas;
  38. canvas = d3.select("body").append("canvas")
  39. .style("display", "none")
  40. .attr("width", "2048px")
  41. .attr("height", "1024px");
  42. context = canvas.node().getContext("2d");
  43. var path = d3.geo.path()
  44. .projection(projection)
  45. .context(context);
  46. context.strokeStyle = "#112";
  47. context.lineWidth = 0.1;
  48. context.fillStyle = color || "#CDB380";
  49. function draw(i,color){
  50. var newGeojson = {features:[geojson.features[i]],type:"FeatureCollection"}
  51. path(newGeojson);
  52. context.fillStyle = color;
  53. context.fill();
  54. context.stroke();
  55. }
  56. //路径顺时针为正
  57. if(geojson.type === "FeatureCollection"){
  58. var points = [];
  59. var color;
  60. var len = geojson.features.length;
  61. /* if(geojson.features[len-1].id == "Europe"){
  62. draw(len-1,"#aa9");
  63. len --;
  64. } */
  65. for(var i=0; i<len; i++){
  66. context.beginPath();
  67. if(geojson.features[i].type2 === "point"){
  68. points.push(i); //point类型的要过后画最上层,不然会被覆盖。
  69. continue;
  70. }
  71. draw(i,"#bbb2a3");
  72. }
  73. for(var i=0; i<points.length; i++){
  74. draw(i,"#aae");
  75. }
  76. }
  77. else{
  78. context.lineWidth = lineWidth || 0;
  79. context.strokeStyle = lineColor || "";
  80. context.beginPath();
  81. path(geojson);
  82. if(color){
  83. context.fill();
  84. }
  85. context.stroke();
  86. }
  87. /* var canv = document.createElement('canvas'); //data的含义
  88. canv.width = 1024; canv.height = 1024;
  89. var ctx = canv.getContext("2d");
  90. ctx.strokeStyle = "#112";
  91. ctx.lineWidth = 1;
  92. ctx.beginPath();
  93. ctx.moveTo(500,500);
  94. var last = [500,500];
  95. for(var i=1;i<data.arcs[175].length; i++){//175
  96. last = [last[0]+data.arcs[175][i][0], last[1]+data.arcs[175][i][1]]
  97. ctx.lineTo(last[0],last[1]);
  98. }
  99. ctx.stroke(); */
  100. // DEBUGGING - Really expensive, disable when done.
  101. // console.log(canvas.node().toDataURL());
  102. texture = new THREE.Texture(canvas.node());
  103. texture.needsUpdate = true;
  104. //var c = canvas[0][0];
  105. //canvasToImg(c,true);
  106. canvas.remove();
  107. return texture;
  108. }
  109. /* world.json的src自加的部分:
  110. 620开始,619是示范
  111. 蒙古
  112. */