64 lines
1.8 KiB
JavaScript
64 lines
1.8 KiB
JavaScript
"use strict";
|
||
define(function (require, exports, module) {
|
||
class A4Utils {
|
||
constructor() {
|
||
let dpi = this.GetDPI();
|
||
console.log("dpi: ", dpi)
|
||
//A0纸尺寸841mm×1189mm
|
||
//
|
||
// A1纸尺寸594mm×841mm
|
||
//
|
||
// A2纸尺寸420mm×594mm
|
||
//
|
||
// A3纸尺寸297mm×420mm
|
||
//
|
||
// A4纸尺寸210mm×297mm
|
||
//
|
||
// A5纸尺寸148mm×210mm
|
||
//
|
||
// A6纸尺寸105mm×148mm
|
||
//
|
||
// A7纸尺寸74mm×105mm
|
||
//
|
||
// A8纸尺寸52mm×74mm
|
||
//
|
||
// A9纸尺寸37mm×52mm
|
||
let width_mm = 210; // A4纸宽度,单位:毫米
|
||
let height_mm = 297; // A4纸高度,单位:毫米
|
||
let width_px = this.MmToPixel(width_mm, dpi);
|
||
let height_px = this.MmToPixel(height_mm, dpi);
|
||
this.width = width_px;
|
||
this.height = height_px;
|
||
}
|
||
|
||
/**
|
||
* A4纸
|
||
* 计算 dpi
|
||
*/
|
||
GetDPI() {
|
||
let tempDiv = document.createElement("div");
|
||
tempDiv.style.width = "1in";
|
||
tempDiv.style.height = "1in";
|
||
tempDiv.style.visibility = "hidden";
|
||
//
|
||
// tempDiv.style.position = "fixed"
|
||
// tempDiv.style.top = "0px"
|
||
// tempDiv.style.left = "0px"
|
||
document.body.appendChild(tempDiv);
|
||
let dpi = tempDiv.offsetWidth;
|
||
// alert(123)
|
||
document.body.removeChild(tempDiv);
|
||
return dpi;
|
||
}
|
||
|
||
MmToPixel(mm, dpi) {
|
||
// 1 inch = 25.4 mm
|
||
let inches = mm / 25.4;
|
||
let pixels = inches * dpi;
|
||
return Math.round(pixels);
|
||
}
|
||
}
|
||
|
||
|
||
module.exports = new A4Utils();
|
||
}) |