Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
e2ff6697
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
5995
Star
90
Fork
162
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
18
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
18
Issue
18
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
e2ff6697
编写于
6月 19, 2024
作者:
X
xty
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
canvas适配物理像素
上级
da73618e
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
12 addition
and
6 deletion
+12
-6
pages/component/canvas/canvas.uvue
pages/component/canvas/canvas.uvue
+12
-6
未找到文件。
pages/component/canvas/canvas.uvue
浏览文件 @
e2ff6697
...
@@ -14,8 +14,9 @@
...
@@ -14,8 +14,9 @@
private vx : number
private vx : number
private vy : number
private vy : number
private radius : number = 5
private radius : number = 5
private devicePixelRatio: Number
constructor(x: number, y: number, vx: number, vy: number, ctx: CanvasRenderingContext2D){
constructor(x: number, y: number, vx: number, vy: number, ctx: CanvasRenderingContext2D
, devicePixelRatio: number
){
this.ctx = ctx
this.ctx = ctx
this.canvasWidth = ctx.canvas.width
this.canvasWidth = ctx.canvas.width
this.canvasHeight = ctx.canvas.height
this.canvasHeight = ctx.canvas.height
...
@@ -23,6 +24,8 @@
...
@@ -23,6 +24,8 @@
this.y = y
this.y = y
this.vx = vx
this.vx = vx
this.vy = vy
this.vy = vy
this.devicePixelRatio = devicePixelRatio
this.radius *= this.devicePixelRatio
}
}
draw() {
draw() {
...
@@ -68,16 +71,17 @@
...
@@ -68,16 +71,17 @@
private layer = 3
private layer = 3
private ballInlayer = 20
private ballInlayer = 20
private interval : number = 0
private interval : number = 0
constructor(ctx : CanvasRenderingContext2D){
constructor(ctx : CanvasRenderingContext2D
, devicePixelRatio: Number
){
this.ctx = ctx
this.ctx = ctx
this.initBall()
this.speed *= devicePixelRatio
this.initBall(devicePixelRatio)
}
}
private getDistance(x: number, y: number) : number{
private getDistance(x: number, y: number) : number{
return Math.pow((Math.pow(x, 2) + Math.pow(y, 2)), 0.5)
return Math.pow((Math.pow(x, 2) + Math.pow(y, 2)), 0.5)
}
}
private initBall() {
private initBall(
devicePixelRatio: Number
) {
let canvasWidth = this.ctx.canvas.width,
let canvasWidth = this.ctx.canvas.width,
canvasHeight = this.ctx.canvas.height
canvasHeight = this.ctx.canvas.height
for (let i = 0; i < this.layer; i++) {
for (let i = 0; i < this.layer; i++) {
...
@@ -90,7 +94,7 @@
...
@@ -90,7 +94,7 @@
y = radius * sin + canvasHeight / 2,
y = radius * sin + canvasHeight / 2,
vx = this.speed * cos,
vx = this.speed * cos,
vy = this.speed * sin
vy = this.speed * sin
ballList.push(new Ball(x, y, vx, vy, ctx))
ballList.push(new Ball(x, y, vx, vy, ctx
, devicePixelRatio
))
}
}
}
}
}
}
...
@@ -118,10 +122,12 @@
...
@@ -118,10 +122,12 @@
var animation : BallAnimation|null = null
var animation : BallAnimation|null = null
onReady(() => {
onReady(() => {
const res = uni.getDeviceInfo();
const devicePixelRatio = res.devicePixelRatio !== null ? res.devicePixelRatio! : 1
let canvas = uni.getElementById("canvas") as UniCanvasElementImpl
let canvas = uni.getElementById("canvas") as UniCanvasElementImpl
let canvasContext = canvas.getContext("2d");
let canvasContext = canvas.getContext("2d");
if(canvasContext != null) {
if(canvasContext != null) {
animation = new BallAnimation(canvasContext)
animation = new BallAnimation(canvasContext
, devicePixelRatio
)
animation?.start()
animation?.start()
} else {
} else {
console.log("canvas.getContext error!!")
console.log("canvas.getContext error!!")
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录