• 采用html2canvas插件,截取百度地图


    最近项目中有个需求,就是一个百度地图页面,需要添加一个截图按钮,截图百度地图页面,生成图片,我百度了,发现html2canvas这个插件能截图,我也试了,但是发现截出来的页面,在地图那块是空白的,最后通过研究,终于实现了地图截图,具体实现如下:

     

    安装

    npm i html2canvas
    

    引入

    import html2canvas from 'html2canvas'

    用法

    1. <template>
    2. <div id="screen" class="dashboard-container">
    3. <el-button type="primary" @click="screenShot">截图el-button>
    4. <div class="map-box">
    5. <div id="map" class="map">div>
    6. div>
    7. div>
    8. template>
    9. <script>
    10. import html2canvas from 'html2canvas';
    11. export default {
    12. name: 'Dashboard',
    13. data () {
    14. return {
    15. map: null,
    16. center: {
    17. lng: 116.403694,
    18. lat: 39.927552
    19. },
    20. zoom: 15,
    21. }
    22. },
    23. mounted () {
    24. this.initMap()
    25. },
    26. methods: {
    27. screenShot () {
    28. const element = document.getElementById('screen')
    29. const options = {
    30. useCORS: true,
    31. // preserveDrawingBuffer: true,
    32. //foreignObjectRendering: true,
    33. allowTaint: true,
    34. }
    35. html2canvas(element, options).then(canvas => {
    36. const png = canvas.toDataURL("image/png") //拿到截图后转换为png图片
    37. const img = document.createElement('img')
    38. img.setAttribute('src', png)
    39. window.document.body.appendChild(img) //将png图片添加到页面验证
    40. console.log('png:', png)
    41. this.downLoad(png)
    42. })
    43. },
    44. downLoad (png) {
    45. //创建一个a标签
    46. var a = document.createElement('a')
    47. //指定下载文件名称
    48. a.href = png;
    49. a.download = '截图'
    50. //a 标签 需要点击触发。所以强制给他分派一个点击事件
    51. //创建一个鼠标事件
    52. let event = document.createEvent("MouseEvents")
    53. // 初始化鼠标事件
    54. event.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
    55. // 指定元素对象触发事件
    56. a.dispatchEvent(event)
    57. },
    58. initMap () {
    59. this.$nextTick(() => {
    60. this.map = new BMapGL.Map('map')
    61. this.map.enableScrollWheelZoom(true)
    62. this.map.centerAndZoom(new BMapGL.Point(this.center.lng, this.center.lat), this.zoom)
    63. let marker = new BMapGL.Marker(new BMapGL.Point(this.center.lng, this.center.lat))
    64. this.map.addOverlay(marker)
    65. })
    66. }
    67. }
    68. }
    69. script>
    70. <style lang="scss" scoped>
    71. .map-box {
    72. margin-top: 30px;
    73. width: 100vw;
    74. height: 500px;
    75. .map {
    76. width: 100%;
    77. height: 100%;
    78. }
    79. }
    80. style>

    效果

     

    修改后

    1. <template>
    2. <div id="screen" class="dashboard-container">
    3. <el-button type="primary" @click="screenShot">截图el-button>
    4. <div class="map-box">
    5. <div id="map" class="map">div>
    6. div>
    7. div>
    8. template>
    9. <script>
    10. import html2canvas from 'html2canvas';
    11. export default {
    12. name: 'Dashboard',
    13. data () {
    14. return {
    15. map: null,
    16. center: {
    17. lng: 116.403694,
    18. lat: 39.927552
    19. },
    20. zoom: 15,
    21. }
    22. },
    23. mounted () {
    24. this.initMap()
    25. },
    26. methods: {
    27. screenShot () {
    28. const element = document.getElementById('screen')
    29. const options = {
    30. useCORS: true,
    31. // preserveDrawingBuffer: true,
    32. //foreignObjectRendering: true,
    33. allowTaint: true,
    34. }
    35. html2canvas(element, options).then(canvas => {
    36. const png = canvas.toDataURL("image/png") //拿到截图后转换为png图片
    37. const img = document.createElement('img')
    38. img.setAttribute('src', png)
    39. window.document.body.appendChild(img) //将png图片添加到页面验证
    40. console.log('png:', png)
    41. this.downLoad(png)
    42. })
    43. },
    44. downLoad (png) {
    45. //创建一个a标签
    46. var a = document.createElement('a')
    47. //指定下载文件名称
    48. a.href = png;
    49. a.download = '截图'
    50. //a 标签 需要点击触发。所以强制给他分派一个点击事件
    51. //创建一个鼠标事件
    52. let event = document.createEvent("MouseEvents")
    53. // 初始化鼠标事件
    54. event.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
    55. // 指定元素对象触发事件
    56. a.dispatchEvent(event)
    57. },
    58. initMap () {
    59. this.$nextTick(() => {
    60. this.map = new BMapGL.Map('map')
    61. this.map.enableScrollWheelZoom(true)
    62. this.map.centerAndZoom(new BMapGL.Point(this.center.lng, this.center.lat), this.zoom)
    63. let marker = new BMapGL.Marker(new BMapGL.Point(this.center.lng, this.center.lat))
    64. this.map.addOverlay(marker)
    65. // const view = new SceneView({
    66. // container: 'map',
    67. // this.map,
    68. // });
    69. // props.setMapView(view);
    70. // 解决html2canvas截图空白问题
    71. HTMLCanvasElement.prototype.getContext = function (origFn) {
    72. return function (type, attributes) {
    73. if (type === 'webgl') {
    74. attributes = Object.assign({}, attributes, {
    75. preserveDrawingBuffer: true,
    76. });
    77. }
    78. return origFn.call(this, type, attributes);
    79. };
    80. }(HTMLCanvasElement.prototype.getContext);
    81. })
    82. }
    83. }
    84. }
    85. script>
    86. <style lang="scss" scoped>
    87. .map-box {
    88. margin-top: 30px;
    89. width: 100vw;
    90. height: 500px;
    91. .map {
    92. width: 100%;
    93. height: 100%;
    94. }
    95. }
    96. style>

     最终效果


     

  • 相关阅读:
    linux中nginx.conf的文件路径
    使用Python进行机器学习:从基础到实战
    APISpace 手机号码归属地API接口案例代码
    “PairElimination“ app Tech Support(URL)
    逮到一个阿里 10 年老 测试开发,聊过之后收益良多...
    Mysql----锁
    Flask设置跨域
    C# 异步编程中的任务取消机制
    POSTGRESQL中的SQL高级应用案例——等你来挑战
    【网络】IP地址和静态路由
  • 原文地址:https://blog.csdn.net/weixin_58528200/article/details/126221290