濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 移動(dòng)端Html5頁(yè)面生成圖片解決方案

移動(dòng)端Html5頁(yè)面生成圖片解決方案

熱門(mén)標(biāo)簽:四川點(diǎn)撥外呼系統(tǒng) 南寧點(diǎn)撥外呼系統(tǒng)哪家公司做的好 當(dāng)涂高德地圖標(biāo)注 成都智能外呼系統(tǒng)平臺(tái) 電銷(xiāo)機(jī)器人電話用什么卡 黃島區(qū)地圖標(biāo)注 鎮(zhèn)江智能外呼系統(tǒng)有效果嗎 云南大理400電話申請(qǐng)官方 江蘇智能電銷(xiāo)機(jī)器人哪家好

現(xiàn)在有很多微信公眾號(hào)運(yùn)營(yíng)活動(dòng),都有生成圖片的需求,生成圖片后可以發(fā)送給好友和發(fā)到朋友圈擴(kuò)散,利于產(chǎn)品的宣傳!

1.生成圖片可以用canvas,但是由于已經(jīng)有了html2canvas這個(gè)開(kāi)源庫(kù),所以為了節(jié)省時(shí)間就沒(méi)有自己寫(xiě)了

github地址: html2canvas

少啰嗦,先看東西?。。?/p>

LiveDemo

/**
     * 根據(jù)window.devicePixelRatio獲取像素比
     */
    function DPR() {
        if (window.devicePixelRatio && window.devicePixelRatio > 1) {
            return window.devicePixelRatio;
        }
        return 1;
    }
    /**
     *  將傳入值轉(zhuǎn)為整數(shù)
     */
    function parseValue(value) {
        return parseInt(value, 10);
    };
    /**
     * 繪制canvas
     */
    async function drawCanvas (selector) {
        // 獲取想要轉(zhuǎn)換的 DOM 節(jié)點(diǎn)
        const dom = document.querySelector(selector);
        const box = window.getComputedStyle(dom);
        // DOM 節(jié)點(diǎn)計(jì)算后寬高
        const width = parseValue(box.width);
        const height = parseValue(box.height);
        // 獲取像素比
        const scaleBy = DPR();
        // 創(chuàng)建自定義 canvas 元素
        var canvas = document.createElement('canvas');
        // 設(shè)定 canvas 元素屬性寬高為 DOM 節(jié)點(diǎn)寬高 * 像素比
        canvas.width = width * scaleBy;
        canvas.height = height * scaleBy;
        // 設(shè)定 canvas css寬高為 DOM 節(jié)點(diǎn)寬高
        canvas.style.width = `${width}px`;
        canvas.style.height = `${height}px`;

        // 獲取畫(huà)筆
        const context = canvas.getContext('2d');

        // 將所有繪制內(nèi)容放大像素比倍
        context.scale(scaleBy, scaleBy);

        let x = width;
        let y = height;
        return await html2canvas(dom, {canvas}).then(function () {
            convertCanvasToImage(canvas, x ,y)
        })
    }

    /**
     * 圖片轉(zhuǎn)base64格式
     */
    function convertCanvasToImage(canvas, x, y) {
        let image = new Image();
        let _container = document.getElementsByClassName('container')[0];
        let _body = document.getElementsByTagName('body')[0];
        image.width = x;
        image.height = y;
        image.src = canvas.toDataURL("image/png");
        _body.removeChild(_container);
        document.body.appendChild(image);
        return image;
    }
    drawCanvas('.container')

2.由于現(xiàn)在的手機(jī)都是高清屏,所以如果你不做處理就會(huì)出現(xiàn)模糊的情況,為什么會(huì)出現(xiàn)模糊的情況?這個(gè)就涉及到設(shè)備像素比 devicePixelRatio js 提供了 window.devicePixelRatio 可以獲取設(shè)備像素比

function DPR() {
        if (window.devicePixelRatio && window.devicePixelRatio > 1) {
            return window.devicePixelRatio;
        }
        return 1;
    }

這個(gè)DPR函數(shù)就是獲取設(shè)備的像素比, 那獲取像素比之后要做什么呢?

var canvas = document.createElement('canvas');
        // 設(shè)定 canvas 元素屬性寬高為 DOM 節(jié)點(diǎn)寬高 * 像素比
        canvas.width = width * scaleBy;
        canvas.height = height * scaleBy;
        // 設(shè)定 canvas css寬高為 DOM 節(jié)點(diǎn)寬高
        canvas.style.width = `${width}px`;
        canvas.style.height = `${height}px`;

        // 獲取畫(huà)筆
        const context = canvas.getContext('2d');

        // 將所有繪制內(nèi)容放大像素比倍
        context.scale(scaleBy, scaleBy);

3.獲取設(shè)備像素比之后將canavs.width 和 canvas.height 去乘以設(shè)備像素比 也就是 scaleBy; 這個(gè)時(shí)候在去設(shè)置canvas.style.width 和 canvas.style.height 為dom的寬和高。想想為什么要這么寫(xiě)?最后在繪制的餓時(shí)候?qū)⑺L制的內(nèi)容放大像素比倍

舉個(gè)例子iphone6S是設(shè)備寬高是375 X 667 ,6S的 window.devicePixelRatio = 物理像素 / dips(2=750/375)所以設(shè)計(jì)師一般給你的設(shè)計(jì)稿是不是都是750*1334的?所以如果按照一比一去繪制在高清屏下就會(huì)模糊,看圖說(shuō)話6S DPR=2

6plus DPR=3

4.最后調(diào)用canvas.toDataURL("image/png");賦值給image.src,由于微信里面無(wú)法保存圖片,所以只能生成圖片文件,調(diào)用微信自帶的長(zhǎng)按保存到圖片到相冊(cè)功能,如圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:淮安 酒泉 佳木斯 十堰 咸寧 南京 西寧 廣西

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《移動(dòng)端Html5頁(yè)面生成圖片解決方案》,本文關(guān)鍵詞  移動(dòng),端,Html5,頁(yè)面,生成,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《移動(dòng)端Html5頁(yè)面生成圖片解決方案》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于移動(dòng)端Html5頁(yè)面生成圖片解決方案的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    榆社县| 田东县| 田阳县| 综艺| 河池市| 上林县| 麻江县| 信丰县| 汾西县| 介休市| 霸州市| 黄陵县| 德化县| 德清县| 探索| 朝阳区| 云梦县| 凌海市| 平乐县| 鄂托克旗| 雷波县| 乌审旗| 象山县| 西宁市| 太谷县| 宁武县| 于都县| 洪洞县| 永宁县| 鄂尔多斯市| 丰镇市| 衡阳市| 镶黄旗| 澄城县| 崇左市| 交口县| 鹤岗市| 西青区| 延津县| 谢通门县| 西乌珠穆沁旗|