Skip to content

Commit 7dcf036

Browse files
committed
add: canvas scale
1 parent c5ba16e commit 7dcf036

File tree

13 files changed

+343
-61
lines changed

13 files changed

+343
-61
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,5 @@ Issues and PRs are welcome!
7878
![2](./screenshot/2.gif)
7979
![3](./screenshot/3.png)
8080
![4](./screenshot/4.png)
81+
![4](./screenshot/5.png)
82+

dist/interface.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ export interface IMathStruct<T> {
1515
copy(obj: T): void;
1616
equal(obj: T): boolean;
1717
}
18+
export declare enum ScaleRadio {
19+
KEEP = "keep",
20+
KEEP_H = "keep_h",
21+
KEEP_W = "keep_w",
22+
IGNORE = "ignore",
23+
EXPAND = "expand"
24+
}
1825
export interface IRapidOptions {
1926
canvas: HTMLCanvasElement;
2027
width?: number;
2128
height?: number;
2229
backgroundColor?: Color;
2330
antialias?: boolean;
31+
devicePixelRatio?: number;
32+
scaleEnable?: boolean;
33+
scaleRadio?: ScaleRadio;
2434
}
2535
export interface IAttribute {
2636
name: string;

dist/rapid.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rapid.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rapid.umd.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/render.d.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ declare class Rapid {
2121
width: number;
2222
height: number;
2323
backgroundColor: Color;
24-
readonly devicePixelRatio: number;
24+
devicePixelRatio: number;
25+
private physicsWidth;
26+
private physicsHeight;
27+
private logicWidth;
28+
private logicHeight;
29+
private scaleEnable;
30+
private scaleRadio;
2531
readonly maxTextureUnits: number;
2632
private readonly defaultColor;
2733
private currentRegion?;
@@ -37,11 +43,12 @@ declare class Rapid {
3743
*/
3844
constructor(options: IRapidOptions);
3945
/**
40-
* Render a tile map layer.
41-
* @param data - The map data to render.
42-
* @param options - The options for rendering the tile map layer.
46+
* @param displayWdith css px size
47+
* @param displayHeight css px size
4348
*/
44-
renderTileMapLayer(data: (number | string)[][], options: ILayerRenderOptions | TileSet): void;
49+
private updateDisplaySize;
50+
private resizeSize;
51+
private updateProjection;
4552
/**
4653
* Initializes WebGL context settings.
4754
* @param gl - The WebGL context.
@@ -96,6 +103,12 @@ declare class Rapid {
96103
* @param cb - The function to render.
97104
*/
98105
render(cb: (dt: number) => void): void;
106+
/**
107+
* Render a tile map layer.
108+
* @param data - The map data to render.
109+
* @param options - The options for rendering the tile map layer.
110+
*/
111+
renderTileMapLayer(data: (number | string)[][], options: ILayerRenderOptions | TileSet): void;
99112
applyCameraTransform(options: ICameraOptions): void;
100113
/**
101114
* Renders a sprite with the specified options.
@@ -156,14 +169,6 @@ declare class Rapid {
156169
* @param options - The options for rendering the circle, including radius, position, color, and segment count.
157170
*/
158171
renderCircle(options: ICircleRenderOptions): void;
159-
/**
160-
* Resizes the canvas and updates the viewport and projection matrix.
161-
* @param width - The new width of the canvas.
162-
* @param height - The new height of the canvas.
163-
*/
164-
resize(logicalWidth: number, logicalHeight: number): void;
165-
private resizeWebglSize;
166-
private updateProjection;
167172
/**
168173
* Clears the canvas with the background color.
169174
* @param bgColor - The background color to clear the canvas with.

docs/examples.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
<div class="sidebar">
103103
<div class="list-nav" id="sidebar-container">
104104
<h3>Examples</h3>
105+
106+
<a class="list-btn" href="/examples/window-scale.html">canvas scale</a>
105107
</div>
106108
</div>
107109

docs/examples/js/window-scale.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import { Rapid, Color, Vec2, ParticleShape, ScaleRadio } from "/Rapid.js/dist/rapid.js";
2+
3+
// 创建Rapid实例
4+
const rapid = new Rapid({
5+
canvas: document.getElementById("game"),
6+
backgroundColor: Color.fromHex("#000000"),
7+
8+
scaleEnable: true,
9+
scaleRadio: ScaleRadio.EXPAND,
10+
11+
width: 500,
12+
height: 500
13+
});
14+
15+
const updateDisplaySize = () => {
16+
rapid.updateDisplaySize(
17+
document.documentElement.clientWidth,
18+
document.documentElement.clientHeight
19+
)
20+
}
21+
22+
window.addEventListener('resize', updateDisplaySize)
23+
updateDisplaySize()
24+
25+
const cat = await rapid.textures.textureFromUrl("./assets/cat.png")
26+
const plane = await rapid.textures.textureFromUrl("./assets/plane.png")
27+
28+
const sprites = []
29+
30+
let count = 0
31+
class Sprite {
32+
constructor() {
33+
this.position = new Vec2(100 * Math.random(), 100 * Math.random())
34+
35+
this.speedY = 10 * Math.random()
36+
this.speedX = 10 * Math.random()
37+
38+
this.texture = Math.random() > 0.5 ? cat : plane
39+
this.color = new Color(
40+
200 + Math.random() * 55,
41+
200 + Math.random() * 55,
42+
200 + Math.random() * 55,
43+
)
44+
45+
this.bounce = Math.random() * 15 + 15
46+
47+
count++
48+
}
49+
50+
51+
render() {
52+
this.position.x += this.speedX
53+
this.position.y += this.speedY
54+
55+
this.speedY += 1
56+
57+
if (this.position.y > 500 - 32) {
58+
this.speedY = -this.bounce
59+
}
60+
61+
if (this.position.x > 500 - 32) {
62+
this.speedX = -5
63+
} else if (this.position.x < 0) {
64+
this.speedX = 5
65+
}
66+
67+
rapid.renderSprite({
68+
texture: this.texture,
69+
position: this.position,
70+
color: this.color,
71+
})
72+
}
73+
}
74+
75+
76+
const addSprite = () => {
77+
sprites.push(new Sprite())
78+
}
79+
80+
for (let index = 0; index < 500; index++) {
81+
addSprite()
82+
}
83+
84+
let add = false
85+
let time = 0
86+
87+
rapid.canvas.onmousedown = () => {
88+
add = true
89+
}
90+
rapid.canvas.onmouseup = () => {
91+
add = false
92+
}
93+
94+
const render = () => {
95+
if (add) {
96+
for (let index = 0; index < 50; index++) {
97+
addSprite()
98+
}
99+
}
100+
101+
time += 0.01
102+
103+
rapid.startRender()
104+
105+
for (let index = 0; index < sprites.length; index++) {
106+
const element = sprites[index]
107+
element.render()
108+
}
109+
110+
rapid.endRender()
111+
}
112+
113+
114+
115+
function animate() {
116+
render()
117+
requestAnimationFrame(animate);
118+
}
119+
120+
animate()

docs/examples/window-scale.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<title>Rapid.js - 粒子系统示例</title>
8+
<style>
9+
body {
10+
margin: 0% !important;
11+
overflow: hidden;
12+
}
13+
14+
#game {
15+
outline: 0px solid black !important;
16+
border-radius: 0 !important;
17+
}
18+
</style>
19+
</head>
20+
21+
<body>
22+
<canvas id="game"></canvas>
23+
24+
<script src="./js/main.js"></script>
25+
<script type="module" src="./js/window-scale.js"></script>
26+
</body>
27+
28+
</html>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rapid-render",
3-
"version": "0.1.14",
3+
"version": "0.1.15",
44
"type": "module",
55
"files": [
66
"dist"

0 commit comments

Comments
 (0)