.
我创建了一个主容器,放入舞台,
后续的显示对象都放入这个容器
.
const exportRoot: createjs.Container = new createjs.Container()
stage.addChild(exportRoot)
.
横竖屏我是通过旋转容器实现的
写了一个 舞台 类来适配
.
import { anim_container, canvas, dom_overlay_container, exportRoot, stage } from "."
.
class Stage
{
static stageWidth: number
static stageHeight: number
static stageType: number
.
static width: number
static height: number
.
static makeResponsive(domContainers: Array<any>): void
{
const w: number = this.width
const h: number = this.height
const pRatio: number = window.devicePixelRatio || 1
const sRatio: number = 1
.
canvas.width = w * pRatio * sRatio
canvas.height = h * pRatio * sRatio
.
domContainers.forEach(function (container)
{
container.style.width = w * sRatio + 'px'
container.style.height = h * sRatio + 'px'
})
stage.scale = pRatio * sRatio
stage.isWebGL == true && stage.updateViewport(canvas.width, canvas.height)
stage.dispatchEvent("resize")
}
static init(): void
{
this.resizeCanvas = this.resizeCanvas.bind(this)
this.resizeCanvas()
window.addEventListener('resize', this.resizeCanvas)
}
static resizeCanvas(): void
{
this.width = document.documentElement.clientWidth
this.height = document.documentElement.clientHeight
.
if (this.width > this.height)
{
this.stageWidth = this.width
this.stageHeight = this.height
this.stageType = 1
exportRoot.rotation = 0
exportRoot.x = 0
}
else
{
this.stageWidth = this.height
this.stageHeight = this.width
this.stageType = 2
exportRoot.rotation = 90
exportRoot.x = this.width
}
this.makeResponsive([canvas, anim_container, dom_overlay_container])
}
}
export { Stage }
.
一开始要先调用舞台类的 init 方法
Stage.init()
.
每个 ui 界面的适配方法
stage.on("resize", this.resizeCanvas, this)
this.resizeCanvas()
.
static resizeCanvas(): void
{
const width: number = Stage.stageWidth / 800
const height: number = Stage.stageHeight / 600
let scale: number = 1
.
width > height ? scale = height : scale = width
.
this.node.scale = scale
this.node.x = Stage.stageWidth / 2
this.node.y = Stage.stageHeight / 2
}
.
这个 ui 界面 原始大小 800 x 600
注册点在中心位置所以是 Stage.stageWidth / 2 Stage.stageHeight / 2
.
我创建了一个主容器,放入舞台,
后续的显示对象都放入这个容器
.
const exportRoot: createjs.Container = new createjs.Container()
stage.addChild(exportRoot)
.
横竖屏我是通过旋转容器实现的
写了一个 舞台 类来适配
.
import { anim_container, canvas, dom_overlay_container, exportRoot, stage } from "."
.
class Stage
{
static stageWidth: number
static stageHeight: number
static stageType: number
.
static width: number
static height: number
.
static makeResponsive(domContainers: Array<any>): void
{
const w: number = this.width
const h: number = this.height
const pRatio: number = window.devicePixelRatio || 1
const sRatio: number = 1
.
canvas.width = w * pRatio * sRatio
canvas.height = h * pRatio * sRatio
.
domContainers.forEach(function (container)
{
container.style.width = w * sRatio + 'px'
container.style.height = h * sRatio + 'px'
})
stage.scale = pRatio * sRatio
stage.isWebGL == true && stage.updateViewport(canvas.width, canvas.height)
stage.dispatchEvent("resize")
}
static init(): void
{
this.resizeCanvas = this.resizeCanvas.bind(this)
this.resizeCanvas()
window.addEventListener('resize', this.resizeCanvas)
}
static resizeCanvas(): void
{
this.width = document.documentElement.clientWidth
this.height = document.documentElement.clientHeight
.
if (this.width > this.height)
{
this.stageWidth = this.width
this.stageHeight = this.height
this.stageType = 1
exportRoot.rotation = 0
exportRoot.x = 0
}
else
{
this.stageWidth = this.height
this.stageHeight = this.width
this.stageType = 2
exportRoot.rotation = 90
exportRoot.x = this.width
}
this.makeResponsive([canvas, anim_container, dom_overlay_container])
}
}
export { Stage }
.
一开始要先调用舞台类的 init 方法
Stage.init()
.
每个 ui 界面的适配方法
stage.on("resize", this.resizeCanvas, this)
this.resizeCanvas()
.
static resizeCanvas(): void
{
const width: number = Stage.stageWidth / 800
const height: number = Stage.stageHeight / 600
let scale: number = 1
.
width > height ? scale = height : scale = width
.
this.node.scale = scale
this.node.x = Stage.stageWidth / 2
this.node.y = Stage.stageHeight / 2
}
.
这个 ui 界面 原始大小 800 x 600
注册点在中心位置所以是 Stage.stageWidth / 2 Stage.stageHeight / 2
.










