H5 图片压缩处理
功能:基本压缩,水印操作,单个批量图片的压缩
实例调用
const fileDom = document.querySelector('#fileUpload')
const afterImg = document.querySelector('#afterImg')
fileDom.onchange = (e) => {
let file = e.target.files[0]
const watermark = [
{
text: '水印一',
color: 'black',
size: 34
},
{
text: '水印二',
color: 'red',
size: 12
}
]
compressImg(file, 0.2, watermark).then((res) => {
console.log(res, '压缩后文件d0-00--')
afterImg.src = res.afterSrc
})
console.log(file, '原文件--d0-00--')
}
const compressImg = (file, quality, watermark = []) => {
const blobToDataURL = (blob, cb) => {
let reader = new FileReader()
reader.onload = (evt) => {
let base64 = evt.target.result
cb(base64)
}
reader.readAsDataURL(blob)
}
function fileToBase64(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => {
resolve(reader.result)
}
reader.onerror = (error) => {
reject(error)
}
})
}
/**
* canvas添加水印
* @param {canvas对象} canvas
* @param {水印文字} text
*/
function addWatermark({
canvas,
width,
height,
color = 'red',
text,
layout = 'topLeft',
level = 20,
size = 18
}) {
const ctx = canvas.getContext('2d')
ctx.fillStyle = color
ctx.textBaseline = 'middle'
const layoutWH = {
topLeft: { w: 0, h: 0 },
topRight: { w: width - level, h: 0 },
bottomLeft: { w: 20, h: height - level },
bottomRight: { w: width - 20, h: height - level },
center: { w: width / 2, h: height / 2 - level }
}
const layerWH = layoutWH[layout]
ctx.fillText(text, layerWH.w, layerWH.h)
ctx.font = `${size}px Arial`
return canvas
}
const fileTypes = [
'image/jpeg',
'image/png',
'image/gif',
'image/x-icon'
]
if (Array.isArray(file)) {
return Promise.all(
Array.from(file).map((item) => compressImg(item, quality))
)
return false
}
return new Promise((resolve, reject) => {
if (!fileTypes.includes(file.type)) {
fileToBase64(file).then((src) => {
resolve({
file: file,
origin: false,
beforeSrc: src,
afterSrc: src,
beforeKB: Number((file.size / 1024).toFixed(2)),
afterKB: Number((file.size / 1024).toFixed(2))
})
})
return false
}
const fileReader = new FileReader()
fileReader.onload = ({ target: { result: src } }) => {
const image = new Image()
image.onload = async () => {
const canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
const ctx = canvas.getContext('2d')
ctx.fillStyle = '#fff'
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.drawImage(image, 0, 0, image.width, image.height)
// 5. 添加水印
if (watermark && watermark.length > 0) {
watermark.forEach((item, index) => {
addWatermark({
canvas,
width: canvas.width,
height: canvas.height - 50,
color: item.color || '#fff',
text: item.text,
sie: item.size || '16',
layout: item.layout || 'bottomLeft',
level: index * 20
})
})
}
const canvasURL = canvas.toDataURL('image/jpeg', quality)
const buffer = atob(canvasURL.split(',')[1])
let length = buffer.length
const bufferArray = new Uint8Array(new ArrayBuffer(length))
while (length--) {
bufferArray[length] = buffer.charCodeAt(length)
}
const miniFile = new File([bufferArray], file.name, {
type: 'image/jpeg'
})
resolve({
file: miniFile,
origin: false,
beforeSrc: src,
afterSrc: canvasURL,
beforeKB: Number((file.size / 1024).toFixed(2)),
afterKB: Number((miniFile.size / 1024).toFixed(2))
})
}
image.src = src
}
fileReader.readAsDataURL(file)
})
}
大数据渲染-切片处理
💡 Tips: window.requestAnimationFrame
范例
'use strict'
let list = document.querySelector('.list')
let total = 100000
let size = 20
let index = 0
const render = (total, index) => {
if (total <= 0) {
return
}
let curPage = Math.min(total, size)
window.requestAnimationFrame(() => {
let fragment = document.createDocumentFragment()
for (let i = 0; i < curPage; ++i) {
let item = document.createElement('li')
item.innerText = `我是${index + i}`
fragment.appendChild(item)
}
list.appendChild(fragment)
render(total - curPage, index + curPage)
})
}
render(total, index)
代码块
💡 Tips:输入
/代码块
或点击上方工具栏点击上方工具栏,选择「代码块」、插入代码卡片。
代码块同时支持多种颜色主题:
export default class QuickSort extends Sort {
sort(originalArray) {
const array = [...originalArray];
if (array.length <= 1) {
return array;
}
// Init left and right arrays.
const leftArray = [];
const rightArray = [];
// Take the first element of array as a pivot.
const pivotElement = array.shift();
const centerArray = [pivotElement];
// Split all array elements between left, center and right arrays.
while (array.length) {
const currentElement = array.shift();
// Call visiting callback.
this.callbacks.visitingCallback(currentElement);
if (this.comparator.equal(currentElement, pivotElement)) {
centerArray.push(currentElement);
} else if (this.comparator.lessThan(currentElement, pivotElement)) {
leftArray.push(currentElement);
} else {
rightArray.push(currentElement);
}
}
// Sort left and right arrays.
const leftArraySorted = this.sort(leftArray);
const rightArraySorted = this.sort(rightArray);
return leftArraySorted.concat(centerArray, rightArraySorted);
}
}
export default class QuickSort extends Sort {
sort(originalArray) {
const array = [...originalArray];
if (array.length <= 1) {
return array;
}
// Init left and right arrays.
const leftArray = [];
const rightArray = [];
// Take the first element of array as a pivot.
const pivotElement = array.shift();
const centerArray = [pivotElement];
// Split all array elements between left, center and right arrays.
while (array.length) {
const currentElement = array.shift();
// Call visiting callback.
this.callbacks.visitingCallback(currentElement);
if (this.comparator.equal(currentElement, pivotElement)) {
centerArray.push(currentElement);
} else if (this.comparator.lessThan(currentElement, pivotElement)) {
leftArray.push(currentElement);
} else {
rightArray.push(currentElement);
}
}
// Sort left and right arrays.
const leftArraySorted = this.sort(leftArray);
const rightArraySorted = this.sort(rightArray);
return leftArraySorted.concat(centerArray, rightArraySorted);
}
}
export default class QuickSort extends Sort {
sort(originalArray) {
const array = [...originalArray];
if (array.length <= 1) {
return array;
}
// Init left and right arrays.
const leftArray = [];
const rightArray = [];
// Take the first element of array as a pivot.
const pivotElement = array.shift();
const centerArray = [pivotElement];
// Split all array elements between left, center and right arrays.
while (array.length) {
const currentElement = array.shift();
// Call visiting callback.
this.callbacks.visitingCallback(currentElement);
if (this.comparator.equal(currentElement, pivotElement)) {
centerArray.push(currentElement);
} else if (this.comparator.lessThan(currentElement, pivotElement)) {
leftArray.push(currentElement);
} else {
rightArray.push(currentElement);
}
}
// Sort left and right arrays.
const leftArraySorted = this.sort(leftArray);
const rightArraySorted = this.sort(rightArray);
return leftArraySorted.concat(centerArray, rightArraySorted);
}
}
数学公式
💡 Tips:输入
/公式
或点击上方工具栏点击上方工具栏,选择「公式」、插入公式卡片。
画板
💡 Tips:输入
/画板
或点击上方工具栏,选择「画板」、绘制流程图、架构图等各种图形。
文本绘图
💡 Tips:输入
/文本绘图
点击上方工具栏,选择「文本绘图」、插入文本绘图卡片。
支持 plantuml、mermaid 等多种格式,点击预览
可看到图形。具体代码样式见说明文档。