canvasframework 0.5.24 → 0.5.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/components/Card.js +98 -52
  2. package/package.json +1 -1
@@ -1,7 +1,6 @@
1
1
  import Component from '../core/Component.js';
2
-
3
2
  /**
4
- * Carte avec système de layout, ombre et bordures arrondies
3
+ * Container avec système de layout et effet d'élévation
5
4
  * @class
6
5
  * @extends Component
7
6
  * @property {Component[]} children - Enfants
@@ -11,36 +10,31 @@ import Component from '../core/Component.js';
11
10
  * @property {string} align - Alignement ('start', 'center', 'end')
12
11
  * @property {string} bgColor - Couleur de fond
13
12
  * @property {number} borderRadius - Rayon des coins
14
- * @property {number} elevation - Élévation (ombre)
15
- * @property {boolean} clipContent - Clip le contenu aux bordures
13
+ * @property {number} elevation - Niveau d'élévation (ombres)
16
14
  */
17
15
  class Card extends Component {
18
16
  /**
19
17
  * Crée une instance de Card
20
18
  * @param {CanvasFramework} framework - Framework parent
21
19
  * @param {Object} [options={}] - Options de configuration
22
- * @param {number} [options.padding=16] - Padding interne
20
+ * @param {number} [options.padding=0] - Padding interne
23
21
  * @param {number} [options.gap=0] - Espacement entre enfants
24
22
  * @param {string} [options.direction='column'] - Direction
25
23
  * @param {string} [options.align='start'] - Alignement
26
- * @param {string} [options.bgColor='#FFFFFF'] - Couleur de fond
27
- * @param {number} [options.borderRadius] - Rayon des coins (auto selon platform)
28
- * @param {number} [options.elevation=2] - Élévation (ombre)
29
- * @param {boolean} [options.clipContent=true] - Clip le contenu
24
+ * @param {string} [options.bgColor='transparent'] - Couleur de fond
25
+ * @param {number} [options.borderRadius=0] - Rayon des coins
26
+ * @param {number} [options.elevation=0] - Niveau d'élévation (0-5)
30
27
  */
31
28
  constructor(framework, options = {}) {
32
29
  super(framework, options);
33
30
  this.children = [];
34
- this.padding = options.padding !== undefined ? options.padding : 16;
31
+ this.padding = options.padding || 0;
35
32
  this.gap = options.gap || 0;
36
- this.direction = options.direction || 'column'; // 'column' ou 'row'
37
- this.align = options.align || 'start'; // 'start', 'center', 'end'
38
- this.bgColor = options.bgColor || '#FFFFFF';
39
- this.borderRadius = options.borderRadius !== undefined
40
- ? options.borderRadius
41
- : (framework.platform === 'material' ? 4 : 12);
42
- this.elevation = options.elevation !== undefined ? options.elevation : 2;
43
- this.clipContent = options.clipContent !== false;
33
+ this.direction = options.direction || 'column';
34
+ this.align = options.align || 'start';
35
+ this.bgColor = options.bgColor || 'transparent';
36
+ this.borderRadius = options.borderRadius || 0;
37
+ this.elevation = options.elevation || 0; // Nouvelle propriété
44
38
  }
45
39
 
46
40
  /**
@@ -59,32 +53,84 @@ class Card extends Component {
59
53
  * @private
60
54
  */
61
55
  layout() {
62
- let currentX = this.padding;
63
- let currentY = this.padding;
56
+ let currentX = this.x + this.padding;
57
+ let currentY = this.y + this.padding;
64
58
 
65
59
  for (let child of this.children) {
66
60
  if (this.direction === 'column') {
67
61
  child.x = currentX;
68
62
  child.y = currentY;
69
63
  if (this.align === 'center') {
70
- child.x = (this.width - child.width) / 2;
64
+ child.x = this.x + (this.width - child.width) / 2;
71
65
  } else if (this.align === 'end') {
72
- child.x = this.width - child.width - this.padding;
66
+ child.x = this.x + this.width - child.width - this.padding;
73
67
  }
74
68
  currentY += child.height + this.gap;
75
69
  } else {
76
70
  child.x = currentX;
77
71
  child.y = currentY;
78
72
  if (this.align === 'center') {
79
- child.y = (this.height - child.height) / 2;
73
+ child.y = this.y + (this.height - child.height) / 2;
80
74
  } else if (this.align === 'end') {
81
- child.y = this.height - child.height - this.padding;
75
+ child.y = this.y + this.height - child.height - this.padding;
82
76
  }
83
77
  currentX += child.width + this.gap;
84
78
  }
85
79
  }
86
80
  }
87
81
 
82
+ /**
83
+ * Génère les paramètres d'ombre selon le niveau d'élévation
84
+ * @param {number} elevation - Niveau d'élévation (0-5)
85
+ * @returns {Object} Configuration de l'ombre
86
+ * @private
87
+ */
88
+ getShadowConfig(elevation) {
89
+ const shadows = [
90
+ { blur: 0, offsetY: 0, color: 'transparent', spread: 0 }, // 0 - pas d'ombre
91
+ { blur: 2, offsetY: 1, color: 'rgba(0,0,0,0.12)', spread: 0 }, // 1 - léger
92
+ { blur: 3, offsetY: 1, color: 'rgba(0,0,0,0.14)', spread: 0 }, // 2 - léger
93
+ { blur: 4, offsetY: 2, color: 'rgba(0,0,0,0.16)', spread: 0 }, // 3 - moyen
94
+ { blur: 6, offsetY: 3, color: 'rgba(0,0,0,0.18)', spread: 0 }, // 4 - moyen
95
+ { blur: 8, offsetY: 4, color: 'rgba(0,0,0,0.20)', spread: 0 }, // 5 - fort
96
+ ];
97
+
98
+ return shadows[Math.min(elevation, shadows.length - 1)];
99
+ }
100
+
101
+ /**
102
+ * Dessine l'effet d'ombre selon l'élévation
103
+ * @param {CanvasRenderingContext2D} ctx - Contexte de dessin
104
+ * @private
105
+ */
106
+ drawShadow(ctx) {
107
+ if (this.elevation <= 0) return;
108
+
109
+ const shadow = this.getShadowConfig(this.elevation);
110
+
111
+ // Sauvegarder l'état du contexte
112
+ ctx.save();
113
+
114
+ // Configurer l'ombre
115
+ ctx.shadowColor = shadow.color;
116
+ ctx.shadowBlur = shadow.blur;
117
+ ctx.shadowOffsetX = 0;
118
+ ctx.shadowOffsetY = shadow.offsetY;
119
+
120
+ // Dessiner un rectangle pour l'ombre
121
+ if (this.borderRadius > 0) {
122
+ ctx.beginPath();
123
+ this.roundRect(ctx, this.x, this.y + shadow.offsetY, this.width, this.height, this.borderRadius);
124
+ ctx.fillStyle = this.bgColor === 'transparent' ? 'white' : this.bgColor;
125
+ ctx.fill();
126
+ } else {
127
+ ctx.fillStyle = this.bgColor === 'transparent' ? 'white' : this.bgColor;
128
+ ctx.fillRect(this.x, this.y + shadow.offsetY, this.width, this.height);
129
+ }
130
+
131
+ ctx.restore();
132
+ }
133
+
88
134
  /**
89
135
  * Dessine la carte et ses enfants
90
136
  * @param {CanvasRenderingContext2D} ctx - Contexte de dessin
@@ -92,14 +138,12 @@ class Card extends Component {
92
138
  draw(ctx) {
93
139
  ctx.save();
94
140
 
95
- // Ombre (elevation)
141
+ // Dessiner l'ombre si elevation > 0
96
142
  if (this.elevation > 0) {
97
- ctx.shadowColor = 'rgba(0, 0, 0, 0.15)';
98
- ctx.shadowBlur = this.elevation * 3;
99
- ctx.shadowOffsetY = this.elevation;
143
+ this.drawShadow(ctx);
100
144
  }
101
145
 
102
- // Background
146
+ // Dessiner le fond de la carte
103
147
  if (this.bgColor !== 'transparent') {
104
148
  ctx.fillStyle = this.bgColor;
105
149
  if (this.borderRadius > 0) {
@@ -111,30 +155,12 @@ class Card extends Component {
111
155
  }
112
156
  }
113
157
 
114
- // Réinitialiser l'ombre
115
- ctx.shadowColor = 'transparent';
116
- ctx.shadowBlur = 0;
117
- ctx.shadowOffsetY = 0;
118
-
119
- // Clipping (optionnel)
120
- if (this.clipContent && this.borderRadius > 0) {
121
- ctx.beginPath();
122
- this.roundRect(ctx, this.x, this.y, this.width, this.height, this.borderRadius);
123
- ctx.clip();
124
- }
125
-
126
- // Children - coordonnées relatives à la Card
127
- ctx.save();
128
- ctx.translate(this.x, this.y);
129
-
158
+ // Dessiner les enfants
130
159
  for (let child of this.children) {
131
- if (child.visible) {
132
- child.draw(ctx);
133
- }
160
+ if (child.visible) child.draw(ctx);
134
161
  }
135
162
 
136
163
  ctx.restore();
137
- ctx.restore();
138
164
  }
139
165
 
140
166
  /**
@@ -148,7 +174,6 @@ class Card extends Component {
148
174
  * @private
149
175
  */
150
176
  roundRect(ctx, x, y, width, height, radius) {
151
- ctx.beginPath();
152
177
  ctx.moveTo(x + radius, y);
153
178
  ctx.lineTo(x + width - radius, y);
154
179
  ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
@@ -158,14 +183,13 @@ class Card extends Component {
158
183
  ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
159
184
  ctx.lineTo(x, y + radius);
160
185
  ctx.quadraticCurveTo(x, y, x + radius, y);
161
- ctx.closePath();
162
186
  }
163
187
 
164
188
  /**
165
189
  * Vérifie si un point est dans les limites
166
190
  * @param {number} x - Coordonnée X
167
191
  * @param {number} y - Coordonnée Y
168
- * @returns {boolean} True si le point est dans la carte
192
+ * @returns {boolean} True si le point est dans la vue
169
193
  */
170
194
  isPointInside(x, y) {
171
195
  return x >= this.x &&
@@ -173,6 +197,28 @@ class Card extends Component {
173
197
  y >= this.y &&
174
198
  y <= this.y + this.height;
175
199
  }
200
+
201
+ /**
202
+ * Définit le niveau d'élévation
203
+ * @param {number} elevation - Nouveau niveau d'élévation (0-5)
204
+ */
205
+ setElevation(elevation) {
206
+ this.elevation = Math.max(0, Math.min(elevation, 5));
207
+ }
208
+
209
+ /**
210
+ * Augmente le niveau d'élévation
211
+ */
212
+ raise() {
213
+ this.setElevation(this.elevation + 1);
214
+ }
215
+
216
+ /**
217
+ * Réduit le niveau d'élévation
218
+ */
219
+ lower() {
220
+ this.setElevation(this.elevation - 1);
221
+ }
176
222
  }
177
223
 
178
224
  export default Card;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "canvasframework",
3
- "version": "0.5.24",
3
+ "version": "0.5.25",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/beyons/CanvasFramework.git"