|
@@ -863,6 +863,121 @@ let NEWSITEEDITOR = (function () {
|
|
|
}
|
|
}
|
|
|
//Fin du nseImageEditor
|
|
//Fin du nseImageEditor
|
|
|
|
|
|
|
|
|
|
+ //nseBgImageEditor
|
|
|
|
|
+ //nseBgImageEditor
|
|
|
|
|
+ function nseBgImageEditor() {
|
|
|
|
|
+ const elems = document.querySelectorAll(".bgimageeditor");
|
|
|
|
|
+
|
|
|
|
|
+ elems.forEach((elem) => {
|
|
|
|
|
+ // 1) Extraction de l'URL du background
|
|
|
|
|
+ elem.addEventListener(
|
|
|
|
|
+ "click",
|
|
|
|
|
+ (e) => {
|
|
|
|
|
+ elem.style.borderTop = "10px solid red";
|
|
|
|
|
+ elem.style.borderBottom = "10px solid red";
|
|
|
|
|
+
|
|
|
|
|
+ const bg = getComputedStyle(elem).backgroundImage; // ex: 'url("…/votre-image.jpg")'
|
|
|
|
|
+ const urlMatch = bg.match(/url\(["']?(.*?)["']?\)/);
|
|
|
|
|
+ if (!urlMatch) return; // pas d'image → on skip
|
|
|
|
|
+ const imgUrl = urlMatch[1];
|
|
|
|
|
+
|
|
|
|
|
+ // 2) Chargement de l'image pour lire sa taille naturelle
|
|
|
|
|
+ const img = new Image();
|
|
|
|
|
+ let Iw, Ih;
|
|
|
|
|
+ img.onload = () => {
|
|
|
|
|
+ Iw = img.naturalWidth;
|
|
|
|
|
+ Ih = img.naturalHeight;
|
|
|
|
|
+ elem.style.backgroundSize = "cover";
|
|
|
|
|
+ };
|
|
|
|
|
+ img.src = imgUrl;
|
|
|
|
|
+
|
|
|
|
|
+ // 3) Variables de drag
|
|
|
|
|
+ let isDragging = false;
|
|
|
|
|
+ let startX, startY;
|
|
|
|
|
+ let origX = 0,
|
|
|
|
|
+ origY = 0;
|
|
|
|
|
+
|
|
|
|
|
+ //Gestion du zoom
|
|
|
|
|
+ let zoom = 1;
|
|
|
|
|
+ const zoomStep = 0.05;
|
|
|
|
|
+ const minZoom = 0.5;
|
|
|
|
|
+ const maxZoom = 3;
|
|
|
|
|
+
|
|
|
|
|
+ // Récupérer la pos courante (px → nombre)
|
|
|
|
|
+ function getBgPos() {
|
|
|
|
|
+ const [x, y] = getComputedStyle(elem).backgroundPosition.split(" ");
|
|
|
|
|
+ return [parseFloat(x) || 0, parseFloat(y) || 0];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ function calculateBackgroundSize(zoom, size, elem) {
|
|
|
|
|
+ elem.style.backgroundSize = zoom * size + "px";
|
|
|
|
|
+ console.log(zoom * size);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ elem.addEventListener("mousedown", (e) => {
|
|
|
|
|
+ e.preventDefault();
|
|
|
|
|
+ isDragging = true;
|
|
|
|
|
+ [origX, origY] = getBgPos();
|
|
|
|
|
+ startX = e.clientX;
|
|
|
|
|
+ startY = e.clientY;
|
|
|
|
|
+ elem.style.cursor = "grabbing";
|
|
|
|
|
+ // on fixe no-repeat pour éviter la répétition
|
|
|
|
|
+ elem.style.backgroundRepeat = "no-repeat";
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ document.addEventListener("mousemove", (e) => {
|
|
|
|
|
+ if (!isDragging) return;
|
|
|
|
|
+ const dx = e.clientX - startX;
|
|
|
|
|
+ const dy = e.clientY - startY;
|
|
|
|
|
+ let nx = origX + dx;
|
|
|
|
|
+ let ny = origY + dy;
|
|
|
|
|
+
|
|
|
|
|
+ // 4) Clamp : une fois l'image chargée
|
|
|
|
|
+ const Cw = elem.clientWidth;
|
|
|
|
|
+ const Ch = elem.clientHeight;
|
|
|
|
|
+ if (Iw && Ih) {
|
|
|
|
|
+ console.log(
|
|
|
|
|
+ `Cadre largeur : ${Cw}, Cadre hauteur : ${Ch}\nImage largeur ${Iw}, Image hauteur ${Ih}`
|
|
|
|
|
+ );
|
|
|
|
|
+ // bornes horizontales
|
|
|
|
|
+
|
|
|
|
|
+ /*
|
|
|
|
|
+ * Ih et Iw : Height et width de l'image d'origine (source)
|
|
|
|
|
+ * Ch et Cw : Height et width du cadre (canvas)
|
|
|
|
|
+ * minX : Taille minimale de la position du background-position sur l'axe horizontal en px.
|
|
|
|
|
+ * maxX : Taille maximale de la position du background-position sur l'axe horizontal en px.
|
|
|
|
|
+ * minY : Taille minimale de la position du background-position sur l'axe vertical en px.
|
|
|
|
|
+ * maxY : Taille minimale de la position du background-position sur l'axe vertical en px.
|
|
|
|
|
+ * nx et ny : Position du background-position manipulé par l'utilisateur.
|
|
|
|
|
+ */
|
|
|
|
|
+ const minX = Math.min(Cw - Iw * zoom, 0);
|
|
|
|
|
+ const maxX = 0;
|
|
|
|
|
+ // bornes verticales
|
|
|
|
|
+ const minY = Math.min(Ch - Ih * zoom, 0);
|
|
|
|
|
+ const maxY = 0;
|
|
|
|
|
+ // clamp X et Y
|
|
|
|
|
+ nx = Math.max(minX, Math.min(maxX, nx));
|
|
|
|
|
+ ny = Math.max(minY, Math.min(maxY, ny));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ elem.style.backgroundPosition = `${0}px ${ny}px`;
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ document.addEventListener("mouseup", () => {
|
|
|
|
|
+ if (!isDragging) return;
|
|
|
|
|
+ isDragging = false;
|
|
|
|
|
+ elem.style.cursor = "";
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // éviter la sélection de texte/image native
|
|
|
|
|
+ elem.addEventListener("dragstart", (e) => e.preventDefault());
|
|
|
|
|
+
|
|
|
|
|
+ },
|
|
|
|
|
+ { once: true }
|
|
|
|
|
+ );
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// to set data-data for every element when opening the page
|
|
// to set data-data for every element when opening the page
|
|
|
$(".siteeditor").each(function () {
|
|
$(".siteeditor").each(function () {
|
|
|
dataData(this);
|
|
dataData(this);
|
|
@@ -888,6 +1003,7 @@ let NEWSITEEDITOR = (function () {
|
|
|
|
|
|
|
|
//nseFileEditor
|
|
//nseFileEditor
|
|
|
//nseImageEditor(false);
|
|
//nseImageEditor(false);
|
|
|
|
|
+ nseBgImageEditor();
|
|
|
|
|
|
|
|
// define the public API
|
|
// define the public API
|
|
|
var API = {};
|
|
var API = {};
|