Puzzle Online
Witaj w świecie puzzli! Wybierz jeden z dostępnych obrazów i spróbuj odtworzyć go z pomieszanych fragmentów. Im więcej elementów zawiera układanka, tym większe wyzwanie. Przeciągaj puzzle, zamieniaj je miejscami i sprawdź, czy uda Ci się ukończyć obraz.
Wzór
Kod po stronie przeglądarki
<style>
.preview{
width:200px;
height:200px;
margin:auto;
border:2px solid var(--bs-secondary);
background-size:cover;
background-position:center;
}
#board{
width:404px;
margin:auto;
display:grid;
grid-template-columns:repeat(4,100px);
grid-template-rows:repeat(4,100px);
gap:1px;
}
.slot{
width:100px;
height:100px;
background:transparent;
border:1px solid var(--bs-secondary);
}
.piece{
width:100%;
height:100%;
cursor:grab;
user-select:none;
}
.piece:hover{
opacity:0.9;
}
#message{
margin-top:20px;
text-align:center;
font-size:1.4rem;
font-weight:bold;
}
</style>
<div id="app">
<div class="row">
<div class="col-lg-4">
<div class="text-center mb-3">
<h2 class="h5">Wzór</h2>
<div id="preview" class="preview"></div>
</div>
</div>
<div class="col-lg-8">
<div id="board"></div>
</div>
</div>
<div id="message"></div>
</div>
<script>
const board = document.getElementById("board");
const message = document.getElementById("message");
const preview = document.getElementById("preview");
let draggedPiece = null;
let shuffled = null;
const puzzleData = {
rows: 4,
cols: 4,
imageBase64: null,
pieces: [
{id:0,row:0,col:0},
{id:1,row:0,col:1},
{id:2,row:0,col:2},
{id:3,row:0,col:3},
{id:4,row:1,col:0},
{id:5,row:1,col:1},
{id:6,row:1,col:2},
{id:7,row:1,col:3},
{id:8,row:2,col:0},
{id:9,row:2,col:1},
{id:10,row:2,col:2},
{id:11,row:2,col:3},
{id:12,row:3,col:0},
{id:13,row:3,col:1},
{id:14,row:3,col:2},
{id:15,row:3,col:3}
]
};
const BOARD_SIZE = 400;
const TILE_SIZE = BOARD_SIZE / puzzleData.cols;
function loadPuzzleData() {
const params = {
token: '72963e85d2e2a5e7161d5f0c9497d312aeef2bec'
};
axios.post(urlSite('public/ajax/app/exec/52'), params, {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
.then(function(response) {
const svg = response.data.svg;
puzzleData.imageBase64 = "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svg))),
preview.style.backgroundImage = `url('${puzzleData.imageBase64}')`;
shuffled = [...puzzleData.pieces].sort(() => Math.random() - 0.5);
createBoard();
}).catch(function(error) {
BootstrapToast.error(error.response.data.message);
});
}
document.addEventListener('DOMContentLoaded', function() {
loadPuzzleData();
})
/* tworzenie planszy */
function createBoard() {
for (let position = 0; position < shuffled.length; position++) {
const slot = document.createElement("div");
slot.className = "slot";
slot.dataset.position = position;
const piece =
createPiece(shuffled[position]);
slot.appendChild(piece);
slot.addEventListener("dragover", e => {
e.preventDefault();
});
slot.addEventListener("drop", e => {
e.preventDefault();
if (!draggedPiece) return;
const sourceSlot = draggedPiece.parentElement;
const targetSlot = slot;
if (sourceSlot === targetSlot) {
return;
}
const targetPiece = targetSlot.firstElementChild;
sourceSlot.appendChild(targetPiece);
targetSlot.appendChild(draggedPiece);
draggedPiece = null;
checkWin();
});
board.appendChild(slot);
}
checkWin();
}
/* tworzenie kafelka */
function createPiece(pieceData) {
const piece = document.createElement("div");
piece.className = "piece";
piece.draggable = true;
piece.dataset.id = pieceData.id;
piece.style.backgroundImage =
`url('${puzzleData.imageBase64}')`;
piece.style.backgroundSize =
`${BOARD_SIZE}px ${BOARD_SIZE}px`;
piece.style.backgroundPosition =
`-${pieceData.col * TILE_SIZE}px -${pieceData.row * TILE_SIZE}px`;
piece.addEventListener("dragstart", () => {
draggedPiece = piece;
});
return piece;
}
/* sprawdzenie zwycięstwa */
function checkWin() {
const slots =
document.querySelectorAll(".slot");
let win = true;
slots.forEach((slot, index) => {
const piece = slot.firstElementChild;
if (!piece) {
win = false;
return;
}
if (Number(piece.dataset.id) !== index) {
win = false;
}
});
if (win) {
message.innerHTML =
"🎉 Gratulacje! Puzzle zostały ułożone.";
message.className =
"text-center text-success fw-bold mt-3";
} else {
message.innerHTML = "";
}
}
</script>
Kod po stronie serwera
Kod serwera niedostępny
Kod po stronie serwera tej aplikacji jest zamknięty i nie jest publicznie dostępny.
Licencja
## BSD-3-Clause License Agreement
BSD-3-Clause
Сopyright (c) 2026 Dariusz Rorat
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Kategorie
Technologie
Masz pomysł na podobną aplikację?
Chętnie pomogę zaplanować MVP, funkcjonalności oraz dalszy rozwój produktu.
Omówmy Twój pomysłNawet wstępny pomysł wystarczy, by rozpocząć rozmowę.