Skip to content

Commit 8f94158

Browse files
committed
v3
- Added ImageAsset.posFromCenter and RectAsset.posFromCenter - Added about -||- in the docs - Made the code in the example thesun.html simpler with the new update
1 parent 23eda0b commit 8f94158

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

Canvas2d.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ Canvas2d.ImageAsset = class extends Canvas2d.Asset {
7070
this.width = width;
7171
this.height = height;
7272
}
73-
x = 0; y = 0; rotation = 0;
73+
x = 0; y = 0; rotation = 0; posFromCenter = false;
7474
draw() {
7575
var ctx = this.parent.context;
7676
ctx.save();
77-
ctx.translate(this.x + this.width / 2, this.y + this.height / 2);
77+
ctx.translate(this.x + (this.posFromCenter ? 0 : (this.width / 2)), this.y + (this.posFromCenter ? 0 : (this.height / 2)));
7878
ctx.rotate(this.rotation * Math.PI / 180);
7979
ctx.drawImage(this.image, this.width / -2, this.height / -2, this.width, this.height);
8080
ctx.restore();
@@ -110,12 +110,12 @@ Canvas2d.RectAsset = class extends Canvas2d.Asset {
110110
this.height = height;
111111
this.color = color;
112112
}
113-
rotation = 0; stroke = false;
113+
rotation = 0; stroke = false; posFromCenter = false;
114114
draw() {
115115
var ctx = this.parent.context;
116116
ctx.save();
117117
ctx[this.stroke ? "strokeStyle" : "fillStyle"] = this.color;
118-
ctx.translate(this.x + this.width / 2, this.y + this.height / 2);
118+
ctx.translate(this.x + (this.posFromCenter ? 0 : (this.width / 2)), this.y + (this.posFromCenter ? 0 : (this.height / 2)));
119119
ctx.rotate(this.rotation * Math.PI / 180);
120120
ctx[this.stroke ? "strokeRect" : "fillRect"](this.width / -2, this.height / -2, this.width, this.height);
121121
ctx.restore();

docs/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ <h2>height<span>: Number</span></h2>
156156
<p>The image height. Assigned value in <a href="#Canvas2d.ImageAsset.constructor">the constructor</a>.</p>
157157
<h2>rotation<span>: Number</span></h2>
158158
<p>The image clockwise rotation (pivot point is image center) in degrees.</p>
159+
<h2>posFromCenter<span>: Boolean</span></h2>
160+
<p>If true, the <a href="#Canvas2d.ImageAsset.x">X</a> and <a href="#Canvas2d.ImageAsset.y">Y</a> will be the coordinates of the image center. Defaults to <code>false</code>.</p>
159161
<h2>draw <span>(): undefined</span></h2>
160162
<p>Draws the image on the canvas. Called from <a href="#Canvas2d.Scene.draw">Canvas2d.Scene.draw</a>.</p>
161163

@@ -202,6 +204,8 @@ <h2>stroke<span>: Boolean</span></h2>
202204
<p>If true, the rectangle will be stroked instead of filled. Defaults to <code>false</code>.</p>
203205
<h2>rotation<span>: Number</span></h2>
204206
<p>The rectangle clockwise rotation (pivot point is image center) in degrees.</p>
207+
<h2>posFromCenter<span>: Boolean</span></h2>
208+
<p>If true, the <a href="#Canvas2d.RectAsset.x">X</a> and <a href="#Canvas2d.RectAsset.y">Y</a> will be the coordinates of the rectangle center. Defaults to <code>false</code>.</p>
205209
<h2>draw <span>(): undefined</span></h2>
206210
<p>Draws the rectangle on the canvas. Called from <a href="#Canvas2d.Scene.draw">Canvas2d.Scene.draw</a>.</p>
207211
</div>

examples/thesun.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<head>
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1">
6-
<title>Canvas2d.js testing</title>
76
<script type="text/javascript" src="../Canvas2d.js"></script>
87
<style type="text/css">
98
body, html {
@@ -21,6 +20,7 @@
2120
canvas = new Canvas2d.Scene(document.getElementById("canvas"));
2221
canvas.background = "#d8d8d8";
2322
sun = new Canvas2d.ImageAsset(document.getElementById("sun"), 300, 300);
23+
sun.posFromCenter = true;
2424
canvas.add(sun);
2525
text = new Canvas2d.TextAsset("The sun!", 0, 0, "100px Arial");
2626
text.stroke = true;
@@ -36,8 +36,8 @@
3636
rect.height = innerHeight - 20;
3737
text.x = innerWidth / 2;
3838
text.y = innerHeight / 2;
39-
sun.x = (innerWidth - sun.width) / 2;
40-
sun.y = (innerHeight - sun.height) / 2;
39+
sun.x = innerWidth / 2;
40+
sun.y = innerHeight / 2;
4141
sun.rotation++;
4242
canvas.width = innerWidth;
4343
canvas.height = innerHeight;

0 commit comments

Comments
 (0)