技术分享
首页
  • JavaScript

    • 构造函数和原型
    • Cookie和Session
    • Object.create(null)和{}
    • TypeScript配置
    • typescript入门到进阶
  • 框架

    • Vue-Router
    • React基础入门
  • 其它

    • Http协议
    • 跨域问题总结
  • 分析Promise实现
  • Axios源码分析
  • Webpack原理
  • vueRouter源码分析
  • Vue

    • Vite快速搭建Vue3+TypeScript项目
    • Cordova打包Vue项目的问题
    • Vue将汉字转为拼音,取出首字母
  • JavaScript

    • new Function
  • 后端

    • Node.js中使用Crypto生成Token
    • Body-Parser处理多层对象的问题
  • 其它

    • 项目Demo汇总
    • Vuepress+Vercel搭建个人站点
    • 项目中能用到的
    • husky规范代码提交
  • Mongoose基础
  • Multer文件上传中间件的使用
  • JavaScript

    • 浅谈两数全等
    • JavaScript进制转换
    • 手写bind,apply,call和new
  • 算法

    • 数组去重和排序
    • 数组扁平化
    • 斐波那契数列
  • JavaScript 数据结构
  • 其它

    • webpack面试题
    • vite面试题
    • svg和canvas的优缺点
    • TypeScript面试题
    • Vue常见面试题
  • 计算机网络

    • 数据链路层
    • 网络层
  • Git的使用
  • Nginx的使用
  • CentOS7安装Nginx
  • 正则表达式
  • SEO搜索引擎优化
  • Serverless介绍
友链
GitHub (opens new window)

鑫生活

总有人要赢,为什么不能是我
首页
  • JavaScript

    • 构造函数和原型
    • Cookie和Session
    • Object.create(null)和{}
    • TypeScript配置
    • typescript入门到进阶
  • 框架

    • Vue-Router
    • React基础入门
  • 其它

    • Http协议
    • 跨域问题总结
  • 分析Promise实现
  • Axios源码分析
  • Webpack原理
  • vueRouter源码分析
  • Vue

    • Vite快速搭建Vue3+TypeScript项目
    • Cordova打包Vue项目的问题
    • Vue将汉字转为拼音,取出首字母
  • JavaScript

    • new Function
  • 后端

    • Node.js中使用Crypto生成Token
    • Body-Parser处理多层对象的问题
  • 其它

    • 项目Demo汇总
    • Vuepress+Vercel搭建个人站点
    • 项目中能用到的
    • husky规范代码提交
  • Mongoose基础
  • Multer文件上传中间件的使用
  • JavaScript

    • 浅谈两数全等
    • JavaScript进制转换
    • 手写bind,apply,call和new
  • 算法

    • 数组去重和排序
    • 数组扁平化
    • 斐波那契数列
  • JavaScript 数据结构
  • 其它

    • webpack面试题
    • vite面试题
    • svg和canvas的优缺点
    • TypeScript面试题
    • Vue常见面试题
  • 计算机网络

    • 数据链路层
    • 网络层
  • Git的使用
  • Nginx的使用
  • CentOS7安装Nginx
  • 正则表达式
  • SEO搜索引擎优化
  • Serverless介绍
友链
GitHub (opens new window)
  • 项目 Demo 汇总
  • node中使用crypto生成token
  • new Function
  • body-parser处理多层对象的问题
  • Vite快速搭建Vue3+TypeScript项目
  • Cordova打包Vue项目的问题
  • Vue将汉字转为拼音,取出首字母
  • 项目中能用到的
    • 实现下载文件
    • html 转图片
    • 解决图片跨域
    • 图片转 base64
    • 图片压缩
  • Vuepress+Vercel搭建个人站点
  • husky规范代码提交
  • 项目
coderly
2021-01-15

项目中能用到的

# 持续更新

# 实现下载文件

  • window.open()可以实现
  • js动态创建a标签,并且插入到文档中,模拟点击实现下载
  • 动态创建表单提交下载

# html 转图片

  • 使用html2canvas插件

# 解决图片跨域

var c = document.querySelector('#myCanvas')
var ctx = c.getContext('2d') // 绘制图片

var img = new Image()
img.crossOrigin = 'anonymous' // 防止跨域出错
img.src =
  'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=684045785,4210542258&fm=11&gp=0.jpg'
img.onload = function() {
  ctx.drawImage(
    img,
    200,
    0,
    img.width,
    img.height,
    500,
    300,
    img.width,
    img.height
  )
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 图片转 base64

function image2Base64(url, disc = 'base64.txt') {
  const fs = require('fs')
  const path = require('path')
  const imageData = fs.readFileSync(path.resolve(__dirname, url))
  if (!imageData) return
  let ext = path.extname(path.resolve(__dirname, url))
  if (ext.startsWith('.')) {
    ext = ext.slice(1)
  }
  const bufferData = Buffer.from(imageData).toString('base64')
  const base64 = `data:image/${ext};base64,${bufferData}`
  fs.writeFileSync(path.resolve(__dirname, disc), base64)
  return base64
}

image2Base64('./docs/.vuepress/public/home.png')
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 图片压缩

  • 通过原生的 input 标签拿到要上传的图片文件
  • 将图片文件转化成 img 元素标签
  • 在 canvas 上压缩绘制该 HTMLImageElement
  • 将 canvas 绘制的图像转成 blob 文件
  • 最后将该 blob 文件传到服务端
<input
  type="file"
  name="img"
  id="image"
  accept="image/*"
  onchange="upload(event)"
/>
1
2
3
4
5
6
7
function upload(event) {
  let files = event.target.files
  let file = files[0]
  readImage(file)
    .then((img) => compressImage(img, 300, 300))
    .then((res) => {
      console.log('res', res)
      // do something
    })
}
function readImage(file) {
  return new Promise((resolve, reject) => {
    let img = new Image()
    let reader = new FileReader() // 读取文件资源
    reader.readAsDataURL(file)
    reader.onload = function(e) {
      img.src = e.target.result
    }
    reader.onerror = function(err) {
      reject(err)
    }
    img.onload = function(e) {
      resolve(img)
    }
    img.onerror = function(err) {
      reject(err)
    }
  })
}
function compressImage(img, w = 1920, h = 1080, type = 'image/png') {
  return new Promise((resolve, reject) => {
    const canvas = document.createElement('canvas')
    const ctx = canvas.getContext('2d')
    const { width, height } = img
    let ratioW = w / width
    let ratioH = h / height
    // 计算比例
    if (ratioW > ratioH) {
      // 宽图片
      h = Math.round(ratioW * height)
    } else {
      // 高图片
      w = Math.round(ratioH * width)
    }
    canvas.width = w
    canvas.height = h
    ctx.clearRect(0, 0, w, h) // 清屏
    ctx.drawImage(img, 0, 0, w, h) // 绘制图片
    canvas.toBlob(function(blob) { // 转为二进制
      resolve(blob)
    }, type)
  })
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
上次更新: 2021/09/13, 15:11:59
Vue将汉字转为拼音,取出首字母
Vuepress+Vercel搭建个人站点

← Vue将汉字转为拼音,取出首字母 Vuepress+Vercel搭建个人站点→

最近更新
01
css
09-13
02
html
09-13
03
README
09-13
更多文章>
Theme by Vdoing | Copyright © 2021-2021 coderly | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式