<div> 截图并且下载

本文介绍了如何通过引入base64.js, jQuery和html2canvas.js库,定义需要截取的页面元素,然后使用html2canvas将页面转换为canvas,最后将canvas内容转化为Base64格式并下载。重点在于展示如何实现将二维码背景区域保存为图片文件的操作。

1、下载        base64.js、jquery-1.11.3.min.js、html2canvas.js        文件

https://download.youkuaiyun.com/download/simplexiaobo/25522312?spm=1001.2014.3001.5503

2、引入上面的文件

<script type="text/javascript" src="__STATIC__/home/js/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="__STATIC__/home/js/html2canvas.js"></script>
<script type="text/javascript" src="__STATIC__/home/js/base64.js"></script>

3、定义需要截取的位置

<!-- 二维码背景区域 -->
<div class="code-box" id="test">
    <img src="home/static/invite-bg.png" class="bg" />
    <div class="user-info">
        <img src="" />
        <div>哈哈哈</div>
    </div>
    <div class="er-code">
        <img src="" />
    </div>
    <div class="invite-code">邀请码:11111</div>
    <div class="btn-box">
        <div class="btn" style="margin-right: 1.25rem;" id="down">保存到手机</div>
        <div class="btn">立即邀请</div>
    </div>
</div>

4、定义画布

<!-- 将页面转换为画布 -->
<canvas id="canvas" style="display: none;"></canvas>

5、定义一个隐藏下载按钮(直接href跳转不会直接下载) 

<a id="btnDownload" class="hide"></a>

6、js方法实现

<script language="javascript">
    // 获取按钮id
    var btnSave = document.getElementById("down");

    // 获取内容id
    var content = document.getElementById("test");
    // 进行canvas生成
    btnSave.onclick = function(){
        html2canvas(content, {
            onrendered: function(canvas) {

                //添加属性
                canvas.setAttribute('id','thecanvas');
                //读取属性值
                //document.getElementById('canvas').innerHTML = '';
                document.getElementById('canvas').appendChild(canvas);

                // 将图片转成base64格式
                var img = canvas.toDataURL("image/jpeg", 1.0); // toDataUrl可以接收2个参数,参数一:图片类型,参数二: 图片质量0-1(不传默认为0.92)
                downBaseImg(img)
                // console.log(img)
                // document.getElementById('base64Img').setAttribute("src", canvas.toDataURL("image/jpeg", 1.0)) // 将base64格式图片显示到页面上
            }
        });
    };
    var btnDownload = document.getElementById("btnDownload");

    var MIME = {
        "application/x-zip-compressed": "zip",
        "application/javascript": "js",
        "text/css": "css",
        "text/plain": "txt",
        "text/html": "html",
        "text/xml": "xml",
        "image/png": "png",
        "image/jpeg": "jpeg",
        "image/gif": "gif",
        "image/svg+xml": "svg"
    };
    //文件名默认当前时间戳
    function downBaseImg(base, name = '下载') {
        var fname = name + "." + MIME[getContentType(base)];
        var blob = getBlob(base);

        if (navigator.msSaveBlob) {
            navigator.msSaveBlob(blob, fname);
        }
        else {
            btnDownload.download = fname;
            btnDownload.href = URL.createObjectURL(blob);
            btnDownload.click();
        }
    }
    /**
     * 获取Blob
     * @param {stirng} base64
     */
    function getBlob(base64) {
        return b64toBlob(getData(base64), getContentType(base64));
    }

    /**
     * 获取文件类型
     * @param {string} base64
     */
    function getContentType(base64) {
        return /data:([^;]*);/i.exec(base64)[1];
    }

    /**
     * 获取base64中的数据
     * @param {string} base64
     */
    function getData(base64) {
        return base64.substr(base64.indexOf("base64,") + 7, base64.length);
    }

    /**
     * base64转Blob
     * @param {string} b64Data
     * @param {string} contentType
     * @param {number} sliceSize
     */
    function b64toBlob(b64Data, contentType, sliceSize) {
        contentType = contentType || '';
        sliceSize = sliceSize || 512;

        var byteCharacters = atob(b64Data);
        var byteArrays = [];

        for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
            var slice = byteCharacters.slice(offset, offset + sliceSize);

            var byteNumbers = new Array(slice.length);
            for (var i = 0; i < slice.length; i++) {
                byteNumbers[i] = slice.charCodeAt(i);
            }

            var byteArray = new Uint8Array(byteNumbers);

            byteArrays.push(byteArray);
        }

        var blob = new Blob(byteArrays, { type: contentType });
        return blob;
    }
</script>

请你帮我把这个运行成可下载的PDF:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Research Hypothesis: MgSO₄ in Chemotherapy-Induced Neuropathic Pain</title> <script src="https://cdn.jsdelivr.net/npm/mermaid@10.6.1/dist/mermaid.min.js"></script> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #f8f9fa; margin: 0; padding: 20px; display: flex; flex-direction: column; align-items: center; } .container { max-width: 1200px; width: 100%; background-color: white; border-radius: 10px; box-shadow: 0 4px 20px rgba(0,0,0,0.1); padding: 30px; margin-top: 20px; } header { text-align: center; margin-bottom: 30px; border-bottom: 2px solid #3498db; padding-bottom: 20px; } h1 { color: #2c3e50; margin-bottom: 10px; } .subtitle { color: #7f8c8d; font-size: 1.2em; margin-top: 0; } .diagram-container { width: 100%; min-height: 500px; background-color: #f5f7fa; border-radius: 8px; padding: 20px; margin: 20px 0; } .legend { display: flex; justify-content: center; flex-wrap: wrap; gap: 15px; margin: 20px 0; padding: 15px; background-color: #e8f4fc; border-radius: 8px; } .legend-item { display: flex; align-items: center; gap: 8px; font-size: 14px; } .color-box { width: 20px; height: 20px; border-radius: 4px; } .key-points { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; margin-top: 30px; } .card { background-color: #f8f9fa; border-left: 4px solid #3498db; padding: 15px; border-radius: 4px; } .card h3 { margin-top: 0; color: #2c3e50; } footer { text-align: center; margin-top: 30px; color: #7f8c8d; font-size: 0.9em; padding-top: 20px; border-top: 1px solid #ecf0f1; } @media print { body { padding: 0; margin: 0; } .container { box-shadow: none; padding: 10px; } .no-print { display: none; } } </style> </head> <body> <div class="container"> <header> <h1>Research Hypothesis: MgSO₄ in Chemotherapy-Induced Neuropathic Pain (CINP)</h1> <p class="subtitle">Mechanism of Action and Protective Effects</p> </header> <div class="diagram-container"> <div class="mermaid"> flowchart TD %% Chemotherapy Induction A[Oxaliplatin<br>Chemotherapy] --> B %% Core Mechanism Pathway subgraph B[Chemotherapy-Induced Neuropathic Pain] direction TB C[TNF-α Release] --> D[TNFR1 Activation] D --> E[Fyn Kinase Activation] E --> F[NR2B Phosphorylation<br>(pTyr1472)] F --> G[NMDA Receptor<br>Hyperactivation] G --> H[Ca²⁺ Influx] H --> I[Neuronal Apoptosis] H --> J[Autophagy Flux<br>Disruption] I --> K[Neuropathic Pain] J --> K end %% Magnesium Intervention L[MgSO₄ Intervention] -->|Inhibits NF-κB Pathway| C L -->|Blocks Ion Channel| G %% Pain Outcome K --> M((Persistent<br>Neuropathic Pain)) %% Style enhancements classDef chem fill:#f8d7da,stroke:#721c24,color:#721c24; classDef process fill:#cfe2ff,stroke:#052c65,color:#052c65; classDef effect fill:#fff3cd,stroke:#856404,color:#856404; classDef pain fill:#d1ecf1,stroke:#0c5460,color:#0c5460; classDef intervention fill:#d4edda,stroke:#155724,color:#155724; class A chem; class B process; class C,D,E,F,G process; class H,I,J effect; class K,M pain; class L intervention; </div> </div> <div class="legend"> <div class="legend-item"> <div class="color-box" style="background-color: #f8d7da;"></div> <span>Chemotherapy Induction</span> </div> <div class="legend-item"> <div class="color-box" style="background-color: #cfe2ff;"></div> <span>Core Molecular Processes</span> </div> <div class="legend-item"> <div class="color-box" style="background-color: #fff3cd;"></div> <span>Cellular Effects</span> </div> <div class="legend-item"> <div class="color-box" style="background-color: #d1ecf1;"></div> <span>Pain Outcome</span> </div> <div class="legend-item"> <div class="color-box" style="background-color: #d4edda;"></div> <span>MgSO₄ Intervention</span> </div> </div> <div class="key-points"> <div class="card"> <h3>Core Pathogenic Mechanism</h3> <p>The "TNF-α → pNR2B-NMDA → Cellular Damage" axis is central to chemotherapy-induced neuropathic pain development:</p> <ul> <li>TNF-α activates TNFR1 receptors</li> <li>Fyn kinase phosphorylates NR2B subunit (pTyr1472)</li> <li>NMDA receptor hyperfunction causes Ca²⁺ overload</li> <li>Leads to neuronal apoptosis & autophagy disruption</li> </ul> </div> <div class="card"> <h3>MgSO₄ Dual-Action Intervention</h3> <p>Magnesium sulfate provides dual protective effects:</p> <ul> <li><strong>Anti-inflammatory action:</strong> Inhibits TNF-α release via NF-κB pathway suppression</li> <li><strong>Receptor modulation:</strong> Blocks NMDA receptor ion channel as non-competitive antagonist</li> <li>Targets both arms of the pathogenic pathway</li> </ul> </div> <div class="card"> <h3>Research Implications</h3> <p>This study will validate:</p> <ul> <li>The causal relationship in the TNF-α/pNR2B pathway</li> <li>MgSO₄ efficacy in preventing CINP development</li> <li>Downstream cellular protection mechanisms</li> <li>Potential for clinical translation</li> </ul> </div> </div> </div> <footer class="no-print"> <p>This diagram illustrates the proposed mechanism of chemotherapy-induced neuropathic pain and the protective effects of magnesium sulfate</p> <p>To save as PDF: Use browser's Print function and select "Save as PDF"</p> </footer> <script> mermaid.initialize({ startOnLoad: true, theme: 'default', flowchart: { useMaxWidth: true, curve: 'linear', nodeSpacing: 50, rankSpacing: 70 }, securityLevel: 'loose' }); </script> </body> </html>
最新发布
07-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值