Word game using PixiJS

I’m trying to create a words of wonder type game using PixiJS. I’m sharing my code below it’s still in development however I can’t seem to figure out how to make it so users can select and drag letters to find words. Right now it only detects the start and the end of the drag and get’s nothing in between. How can I fix this? Thanks!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>window.onload = () => {
const container = document.getElementById('canvas-container');
const app = new PIXI.Application({
width: container.clientWidth * 2,
height: container.clientHeight * 2,
backgroundColor: 0x1099bb,
});
container.appendChild(app.view);
const background = PIXI.Sprite.from('https://cdn.glitch.global/b457296f-514e-47e5-82c6-28bdeeda16ca/background.png?v=1716472339788');
background.width = app.screen.width;
background.height = app.screen.height;
app.stage.addChild(background);
const gridContent = [
[null, null, null, null],
[null, '', null, ''],
[null, null, null, ''],
];
const gridSize = app.screen.width / 5.3;
const gridPadding = gridSize / 10;
const letters = ['L', 'G', 'O', 'D'];
const angleIncrement = (Math.PI * 2) / letters.length;
const circleCenterX = app.screen.width / 2;
const circleCenterY = app.screen.height - app.screen.width / 3 - 170;
const circleRadius = app.screen.width / 3.3;
const letterRadius = circleRadius / 4.2;
const letterColors = ['#ff9933', '#ff9933', '#ff9933', '#ff9933'];
const numRows = gridContent.length;
const numCols = gridContent[0].length;
const gridStartX = (app.screen.width - (numCols * (gridSize + gridPadding))) / 2;
const gridGapY = 120;
const gridStartY = gridGapY;
// Define an array to keep track of selected letters
let selectedLettersArray = [];
function handleLetterDragging(letter) {
console.log('Dragging:', letter);
// Check if the letter has not been selected already
if (!selectedLettersArray.includes(letter)) {
selectedLettersArray.push(letter);
console.log('Selected letters array:', selectedLettersArray);
checkForValidWord();
}
}
function checkForValidWord() {
const selectedLetters = selectedLettersArray.join('');
console.log('Checking for valid word:', selectedLetters);
const foundAnswer = correctAnswers.find(answer => selectedLetters.endsWith(answer));
if (foundAnswer) {
console.log('Found valid word:', foundAnswer);
fillGrid(foundAnswer, selectedLetters.length - foundAnswer.length);
// Reset selected letters array after finding a valid word
selectedLettersArray = [];
}
}
function drawLetterWheel() {
const shuffleButtonUrl = 'images/shuffle.png';
const shuffleButtonSize = 65;
const circle = new PIXI.Graphics();
circle.beginFill(0xfff7e6, 0.7);
circle.drawCircle(circleCenterX, circleCenterY, circleRadius);
circle.endFill();
app.stage.addChild(circle);
letters.forEach((letter, index) => {
const angle = index * angleIncrement;
const letterShiftFactor = 0.8;
const x = circleCenterX + Math.cos(angle) * (circleRadius - letterRadius) * letterShiftFactor;
const y = circleCenterY + Math.sin(angle) * (circleRadius - letterRadius) * letterShiftFactor;
const letterText = new PIXI.Text(letter, {
fontSize: letterRadius * 2,
fill: letterColors[index],
fontFamily: ['Poetsen One', 'Roboto', 'sans-serif']
});
letterText.anchor.set(0.5);
letterText.position.set(x, y);
app.stage.addChild(letterText);
// Enable dragging for each letter
letterText.interactive = true;
letterText.buttonMode = true;
letterText
.on('pointerdown', onDragStart)
.on('pointerup', onDragEnd)
.on('pointerupoutside', onDragEnd);
function onDragStart() {
console.log('Drag start:', letter);
handleLetterDragging(letter);
letterText.interactive = false; // Disable interactivity after selection
}
function onDragEnd() {
console.log('Drag end:', letter);
letterText.interactive = true; // Re-enable interactivity after dragging
}
});
const shuffleButton = PIXI.Sprite.from(shuffleButtonUrl);
shuffleButton.anchor.set(0.5);
shuffleButton.width = shuffleButton.height = shuffleButtonSize;
shuffleButton.position.set(circleCenterX, circleCenterY);
shuffleButton.interactive = true;
shuffleButton.buttonMode = true;
shuffleButton.on('pointerdown', () => {
shuffleLetters();
});
app.stage.addChild(shuffleButton);
}
const correctAnswers = ['GOLD', 'DOG', 'GOD', 'LOG']; // Define correct answers here
function drawGrid() {
for (let row = 0; row < numRows; row++) {
for (let col = 0; col < numCols; col++) {
if (gridContent[row][col] === null) {
const cell = new PIXI.Graphics();
cell.beginFill(0xfff7e6);
cell.drawRoundedRect(0, 0, gridSize, gridSize, 20);
cell.endFill();
cell.position.set(gridStartX + col * (gridSize + gridPadding), gridStartY + row * (gridSize + gridPadding));
app.stage.addChild(cell);
}
}
}
}
function fillGrid(answer, startIndex) {
for (let i = 0; i < answer.length; i++) {
const row = Math.floor(startIndex / 4);
const col = startIndex % 4;
const cell = app.stage.children.find(child => child instanceof PIXI.Graphics &&
child.position.x === col * (gridSize + gridPadding) + gridStartX &&
child.position.y === row * (gridSize + gridPadding) + gridStartY);
if (cell) {
const text = new PIXI.Text(answer[i], { fill: 'black' });
text.anchor.set(0.5);
text.position.set(cell.x + gridSize / 2, cell.y + gridSize / 2);
app.stage.addChild(text);
}
startIndex++;
}
}
function shuffleLetters() {
const letters = ['L', 'G', 'O', 'D'];
for (let i = letters.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[letters[i], letters[j]] = [letters[j], letters[i]];
}
const letterTexts = app.stage.children.filter(child => child instanceof PIXI.Text);
letters.forEach((letter, index) => {
letterTexts[index].text = letter;
letterTexts[index].interactive = true; // Re-enable interactivity after shuffling
});
}
drawGrid();
drawLetterWheel();
</code>
<code>window.onload = () => { const container = document.getElementById('canvas-container'); const app = new PIXI.Application({ width: container.clientWidth * 2, height: container.clientHeight * 2, backgroundColor: 0x1099bb, }); container.appendChild(app.view); const background = PIXI.Sprite.from('https://cdn.glitch.global/b457296f-514e-47e5-82c6-28bdeeda16ca/background.png?v=1716472339788'); background.width = app.screen.width; background.height = app.screen.height; app.stage.addChild(background); const gridContent = [ [null, null, null, null], [null, '', null, ''], [null, null, null, ''], ]; const gridSize = app.screen.width / 5.3; const gridPadding = gridSize / 10; const letters = ['L', 'G', 'O', 'D']; const angleIncrement = (Math.PI * 2) / letters.length; const circleCenterX = app.screen.width / 2; const circleCenterY = app.screen.height - app.screen.width / 3 - 170; const circleRadius = app.screen.width / 3.3; const letterRadius = circleRadius / 4.2; const letterColors = ['#ff9933', '#ff9933', '#ff9933', '#ff9933']; const numRows = gridContent.length; const numCols = gridContent[0].length; const gridStartX = (app.screen.width - (numCols * (gridSize + gridPadding))) / 2; const gridGapY = 120; const gridStartY = gridGapY; // Define an array to keep track of selected letters let selectedLettersArray = []; function handleLetterDragging(letter) { console.log('Dragging:', letter); // Check if the letter has not been selected already if (!selectedLettersArray.includes(letter)) { selectedLettersArray.push(letter); console.log('Selected letters array:', selectedLettersArray); checkForValidWord(); } } function checkForValidWord() { const selectedLetters = selectedLettersArray.join(''); console.log('Checking for valid word:', selectedLetters); const foundAnswer = correctAnswers.find(answer => selectedLetters.endsWith(answer)); if (foundAnswer) { console.log('Found valid word:', foundAnswer); fillGrid(foundAnswer, selectedLetters.length - foundAnswer.length); // Reset selected letters array after finding a valid word selectedLettersArray = []; } } function drawLetterWheel() { const shuffleButtonUrl = 'images/shuffle.png'; const shuffleButtonSize = 65; const circle = new PIXI.Graphics(); circle.beginFill(0xfff7e6, 0.7); circle.drawCircle(circleCenterX, circleCenterY, circleRadius); circle.endFill(); app.stage.addChild(circle); letters.forEach((letter, index) => { const angle = index * angleIncrement; const letterShiftFactor = 0.8; const x = circleCenterX + Math.cos(angle) * (circleRadius - letterRadius) * letterShiftFactor; const y = circleCenterY + Math.sin(angle) * (circleRadius - letterRadius) * letterShiftFactor; const letterText = new PIXI.Text(letter, { fontSize: letterRadius * 2, fill: letterColors[index], fontFamily: ['Poetsen One', 'Roboto', 'sans-serif'] }); letterText.anchor.set(0.5); letterText.position.set(x, y); app.stage.addChild(letterText); // Enable dragging for each letter letterText.interactive = true; letterText.buttonMode = true; letterText .on('pointerdown', onDragStart) .on('pointerup', onDragEnd) .on('pointerupoutside', onDragEnd); function onDragStart() { console.log('Drag start:', letter); handleLetterDragging(letter); letterText.interactive = false; // Disable interactivity after selection } function onDragEnd() { console.log('Drag end:', letter); letterText.interactive = true; // Re-enable interactivity after dragging } }); const shuffleButton = PIXI.Sprite.from(shuffleButtonUrl); shuffleButton.anchor.set(0.5); shuffleButton.width = shuffleButton.height = shuffleButtonSize; shuffleButton.position.set(circleCenterX, circleCenterY); shuffleButton.interactive = true; shuffleButton.buttonMode = true; shuffleButton.on('pointerdown', () => { shuffleLetters(); }); app.stage.addChild(shuffleButton); } const correctAnswers = ['GOLD', 'DOG', 'GOD', 'LOG']; // Define correct answers here function drawGrid() { for (let row = 0; row < numRows; row++) { for (let col = 0; col < numCols; col++) { if (gridContent[row][col] === null) { const cell = new PIXI.Graphics(); cell.beginFill(0xfff7e6); cell.drawRoundedRect(0, 0, gridSize, gridSize, 20); cell.endFill(); cell.position.set(gridStartX + col * (gridSize + gridPadding), gridStartY + row * (gridSize + gridPadding)); app.stage.addChild(cell); } } } } function fillGrid(answer, startIndex) { for (let i = 0; i < answer.length; i++) { const row = Math.floor(startIndex / 4); const col = startIndex % 4; const cell = app.stage.children.find(child => child instanceof PIXI.Graphics && child.position.x === col * (gridSize + gridPadding) + gridStartX && child.position.y === row * (gridSize + gridPadding) + gridStartY); if (cell) { const text = new PIXI.Text(answer[i], { fill: 'black' }); text.anchor.set(0.5); text.position.set(cell.x + gridSize / 2, cell.y + gridSize / 2); app.stage.addChild(text); } startIndex++; } } function shuffleLetters() { const letters = ['L', 'G', 'O', 'D']; for (let i = letters.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [letters[i], letters[j]] = [letters[j], letters[i]]; } const letterTexts = app.stage.children.filter(child => child instanceof PIXI.Text); letters.forEach((letter, index) => { letterTexts[index].text = letter; letterTexts[index].interactive = true; // Re-enable interactivity after shuffling }); } drawGrid(); drawLetterWheel(); </code>
window.onload = () => {
const container = document.getElementById('canvas-container');
const app = new PIXI.Application({
    width: container.clientWidth * 2,
    height: container.clientHeight * 2,
    backgroundColor: 0x1099bb,
});
container.appendChild(app.view);

const background = PIXI.Sprite.from('https://cdn.glitch.global/b457296f-514e-47e5-82c6-28bdeeda16ca/background.png?v=1716472339788');
background.width = app.screen.width;
background.height = app.screen.height;
app.stage.addChild(background);

const gridContent = [
    [null, null, null, null],
    [null, '', null, ''],
    [null, null, null, ''],
];

const gridSize = app.screen.width / 5.3;
const gridPadding = gridSize / 10;
const letters = ['L', 'G', 'O', 'D'];
const angleIncrement = (Math.PI * 2) / letters.length;
const circleCenterX = app.screen.width / 2;
const circleCenterY = app.screen.height - app.screen.width / 3 - 170;
const circleRadius = app.screen.width / 3.3;
const letterRadius = circleRadius / 4.2;
const letterColors = ['#ff9933', '#ff9933', '#ff9933', '#ff9933'];
const numRows = gridContent.length;
const numCols = gridContent[0].length;
const gridStartX = (app.screen.width - (numCols * (gridSize + gridPadding))) / 2;
const gridGapY = 120;
const gridStartY = gridGapY;

// Define an array to keep track of selected letters
let selectedLettersArray = [];

function handleLetterDragging(letter) {
    console.log('Dragging:', letter);
    // Check if the letter has not been selected already
    if (!selectedLettersArray.includes(letter)) {
        selectedLettersArray.push(letter);
        console.log('Selected letters array:', selectedLettersArray);
        checkForValidWord();
    }
}

function checkForValidWord() {
    const selectedLetters = selectedLettersArray.join('');
    console.log('Checking for valid word:', selectedLetters);
    const foundAnswer = correctAnswers.find(answer => selectedLetters.endsWith(answer));
    if (foundAnswer) {
        console.log('Found valid word:', foundAnswer);
        fillGrid(foundAnswer, selectedLetters.length - foundAnswer.length);
        // Reset selected letters array after finding a valid word
        selectedLettersArray = [];
    }
}

function drawLetterWheel() {
    const shuffleButtonUrl = 'images/shuffle.png';
    const shuffleButtonSize = 65;

    const circle = new PIXI.Graphics();
    circle.beginFill(0xfff7e6, 0.7);
    circle.drawCircle(circleCenterX, circleCenterY, circleRadius);
    circle.endFill();
    app.stage.addChild(circle);

    letters.forEach((letter, index) => {
        const angle = index * angleIncrement;
        const letterShiftFactor = 0.8;
        const x = circleCenterX + Math.cos(angle) * (circleRadius - letterRadius) * letterShiftFactor;
        const y = circleCenterY + Math.sin(angle) * (circleRadius - letterRadius) * letterShiftFactor;

        const letterText = new PIXI.Text(letter, {
            fontSize: letterRadius * 2,
            fill: letterColors[index],
            fontFamily: ['Poetsen One', 'Roboto', 'sans-serif']
        });
        letterText.anchor.set(0.5);
        letterText.position.set(x, y);
        app.stage.addChild(letterText);

        // Enable dragging for each letter
        letterText.interactive = true;
        letterText.buttonMode = true;

        letterText
            .on('pointerdown', onDragStart)
            .on('pointerup', onDragEnd)
            .on('pointerupoutside', onDragEnd);

        function onDragStart() {
            console.log('Drag start:', letter);
            handleLetterDragging(letter);
            letterText.interactive = false; // Disable interactivity after selection
        }

        function onDragEnd() {
            console.log('Drag end:', letter);
            letterText.interactive = true; // Re-enable interactivity after dragging
        }
    });

    const shuffleButton = PIXI.Sprite.from(shuffleButtonUrl);
    shuffleButton.anchor.set(0.5);
    shuffleButton.width = shuffleButton.height = shuffleButtonSize;
    shuffleButton.position.set(circleCenterX, circleCenterY);
    shuffleButton.interactive = true;
    shuffleButton.buttonMode = true;
    shuffleButton.on('pointerdown', () => {
        shuffleLetters();
    });
    app.stage.addChild(shuffleButton);
}

const correctAnswers = ['GOLD', 'DOG', 'GOD', 'LOG']; // Define correct answers here

function drawGrid() {


    for (let row = 0; row < numRows; row++) {
        for (let col = 0; col < numCols; col++) {
            if (gridContent[row][col] === null) {
                const cell = new PIXI.Graphics();
                cell.beginFill(0xfff7e6);
                cell.drawRoundedRect(0, 0, gridSize, gridSize, 20);
                cell.endFill();
                cell.position.set(gridStartX + col * (gridSize + gridPadding), gridStartY + row * (gridSize + gridPadding));
                app.stage.addChild(cell);
            }
        }
    }
}

function fillGrid(answer, startIndex) {
    for (let i = 0; i < answer.length; i++) {
        const row = Math.floor(startIndex / 4);
        const col = startIndex % 4;
        const cell = app.stage.children.find(child => child instanceof PIXI.Graphics &&
            child.position.x === col * (gridSize + gridPadding) + gridStartX &&
            child.position.y === row * (gridSize + gridPadding) + gridStartY);
        if (cell) {
            const text = new PIXI.Text(answer[i], { fill: 'black' });
            text.anchor.set(0.5);
            text.position.set(cell.x + gridSize / 2, cell.y + gridSize / 2);
            app.stage.addChild(text);
        }
        startIndex++;
    }
}

function shuffleLetters() {
    const letters = ['L', 'G', 'O', 'D'];
    for (let i = letters.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [letters[i], letters[j]] = [letters[j], letters[i]];
    }

    const letterTexts = app.stage.children.filter(child => child instanceof PIXI.Text);
    letters.forEach((letter, index) => {
        letterTexts[index].text = letter;
        letterTexts[index].interactive = true; // Re-enable interactivity after shuffling
    });
}

drawGrid();
drawLetterWheel();

};

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật