Skip to content
This repository has been archived by the owner on May 12, 2023. It is now read-only.

Commit

Permalink
fix: scale canvas to adapt dpr (#4)
Browse files Browse the repository at this point in the history
* fix: scale canvas to adapt dpr

小程序在手机端由于 devicePixelRatio,canvas 如果不根据设备 dpr 进行 x2 x3 的渲染会导致糊

https://juejin.cn/post/6982019644169125924
https://developers.weixin.qq.com/miniprogram/dev/component/canvas.html#Canvas-2D-%E7%A4%BA%E4%BE%8B%E4%BB%A3%E7%A0%81

* Update mp.tsx

Co-authored-by: tony chen <tony.zq.chen@gmail.com>
  • Loading branch information
lzfxxx and zhiqingchen authored Dec 29, 2022
1 parent bdd4c15 commit e5066a0
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/lib/mp.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Canvas } from '@tarojs/components'
import { createSelectorQuery } from '@tarojs/taro'
import { createSelectorQuery, getSystemInfoSync } from '@tarojs/taro'
import lottie from 'lottie-miniprogram'
import type { AnimatedLottieViewProps } from 'lottie-react-native'
import React, { Component } from 'react'
Expand Down Expand Up @@ -72,8 +72,13 @@ class LottieView extends Component<AnimatedLottieViewProps, LottieViewState> {
try {
const canvas = res.node
const context = canvas.getContext('2d')
canvas.width = parseFloat(width)
canvas.height = parseFloat(height)

// scale canvas to adapt dpr
const dpr = getSystemInfoSync().pixelRatio
canvas.width = parseFloat(width) * dpr
canvas.height = parseFloat(height) * dpr
context.scale(dpr, dpr)

lottie.setup(canvas)
this.animation = lottie.loadAnimation({
animationData: source,
Expand Down

0 comments on commit e5066a0

Please sign in to comment.