Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Touch-Color-Changing Tiles for Babies #33

Merged
merged 31 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cc9ce40
Create BabyTouchScreen.html
kimpro82 Jan 18, 2024
1c45123
Create BabyTouchScreen.css
kimpro82 Jan 18, 2024
bca4d05
Create BabyTouchScreen.ts
kimpro82 Jan 18, 2024
dc1471c
Upload sample.png
kimpro82 Jan 18, 2024
8b39eb7
Upload BabyTouchScreen.js
kimpro82 Jan 18, 2024
880e36b
Update BabyTouchScreen.html
kimpro82 Jan 18, 2024
d0c145e
Update BabyTouchScreen.html
kimpro82 Jan 18, 2024
6553d5a
Update BabyTouchScreen.css
kimpro82 Jan 18, 2024
b1b0093
Update BabyTouchScreen.ts
kimpro82 Jan 18, 2024
ff91709
Upload BabyTouchScreen.js
kimpro82 Jan 18, 2024
1be424e
Create index.html
kimpro82 Jan 18, 2024
5d97d10
Update BabyTouchScreen.ts
kimpro82 Jan 18, 2024
5eae3ec
Update BabyTouchScreen.js
kimpro82 Jan 18, 2024
3df795f
Upload Images/santa.jpg
kimpro82 Jan 18, 2024
65dca5c
Update BabyTouchScreen.ts
kimpro82 Jan 18, 2024
4d909e9
Update BabyTouchScreen.js
kimpro82 Jan 18, 2024
f3127ad
Update BabyTouchScreen.js
kimpro82 Jan 18, 2024
763f31e
Upload BabyTouchScreen_Sample.gif
kimpro82 Jan 19, 2024
b80a9f8
Upload BabyTouchScreen_Santa.gif
kimpro82 Jan 19, 2024
c86547a
Update BabyTouchScreen.html
kimpro82 Jan 19, 2024
ad06510
Update BabyTouchScreen.ts
kimpro82 Jan 19, 2024
949300b
Update BabyTouchScreen.ts
kimpro82 Jan 19, 2024
7a7f754
Create README.md
kimpro82 Jan 19, 2024
b5296ad
Update README.md
kimpro82 Jan 19, 2024
0d5c225
Update /README.md
kimpro82 Jan 19, 2024
af1ce91
Upload TouchColorChangingTiles_Sample.gif
kimpro82 Jan 19, 2024
cb2f24b
Upload TouchColorChangingTiles_Santa.gif
kimpro82 Jan 19, 2024
b21f7ea
Rename `*BabyTouchScreen*` to `*TouchColorChangingTiles*`
kimpro82 Jan 19, 2024
fc54fbd
Update TouchColorChangingTiles.ts
kimpro82 Jan 19, 2024
d13283d
Update TouchColorChangingTiles.js
kimpro82 Jan 19, 2024
3f97ac4
Update README.md
kimpro82 Jan 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
Rock N' Roll Baby!


## [Idea Generator](/IdeaGenerator)
## [Touch-Color-Changing Tiles for Babies](/TouchColorChangingTiles/)

- [Touch-Color-Changing Tiles for Babies (2024.01.18)](/TouchColorChangingTiles/README.md#touch-color-changing-tiles-for-babies-20240118)


## [Idea Generator](/IdeaGenerator/)

- [Idea Generator v0.22 (2022.06.06)](/IdeaGenerator/readme.md#idea-generator-v022-20220606)
- [Idea Generator v0.21 (2022.06.05)](/IdeaGenerator/readme.md#idea-generator-v021-20220605)
Expand All @@ -13,7 +18,7 @@ Rock N' Roll Baby!
- [Idea Generator v0.10 (2022.05.18)](/IdeaGenerator/readme.md#idea-generator-v010-20220518)


## [Wedding Anniversary](/WeddingAnniversary)
## [Wedding Anniversary](/WeddingAnniversary/)

- [8th Wedding Anniversary (2023.03.07)](/WeddingAnniversary/README.md#8th-wedding-anniversary-20230307)
- [7th Wedding Anniversary (2022.03.07)](/WeddingAnniversary/README.md#7th-wedding-anniversary-20220307)
Expand All @@ -22,6 +27,6 @@ Rock N' Roll Baby!
- [5th Wedding Anniversary (2020.03.07)](/WeddingAnniversary/README.md#5th-wedding-anniversary-20200307)


## [Baby Focus Book](/BabyFocusBook)
## [Baby Focus Book](/BabyFocusBook/)

- [Black & White 1 (2021.06.15)](/BabyFocusBook/README.md#black--white-1-20210615)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added TouchColorChangingTiles/Images/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added TouchColorChangingTiles/Images/santa.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
193 changes: 193 additions & 0 deletions TouchColorChangingTiles/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# [Touch-Color-Changing Tiles for Babies](../README.md#)

But the son looked for YouTube before long


### List

- [Touch-Color-Changing Tiles for Babies (2024.01.18)](#touch-color-changing-tiles-for-babies-20240118)


## [Touch-Color-Changing Tiles for Babies (2024.01.18)](#list)

- Features
- Dynamic application of the number and size of cells
- Color change while traversing a pastel tone palette with touch input
- Apply the same `mousedown` and `touchstart` actions for smooth testing

- Future Improvements
- Addition of more dynamic animation effects
- Clear conditions such as color uniformity
- Additional options such as the number of colors, cell size, and various photos

![TouchColorChangingTiles_Santa.gif](./Images/TouchColorChangingTiles_Santa.gif)

- Code
<details>
<summary>TouchColorChangingTiles.html</summary>

```html
<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="TouchColorChangingTiles.css">
<script defer src="TouchColorChangingTiles.js"></script>
<title>Touch-Color-Changing Tiles for My Son</title>
</head>

<body>
<canvas id="responsiveGridCanvas"></canvas>
</body>

</html>
```
</details>
<details>
<summary>TouchColorChangingTiles.css</summary>

```css
body, html {
background-color: #333;
margin: 0;
padding: 0;
overflow: hidden;
}
```
```css
canvas {
display: block;
margin: auto;
}
```
</details>
<details>
<summary>TouchColorChangingTiles.ts</summary>

```ts
let firstRun: boolean = true;

const canvas = document.getElementById('responsiveGridCanvas') as HTMLCanvasElement;
const ctx = canvas.getContext('2d');

const defaultCellSize = 120; // Default cell size
let actualCellSizeX: number = defaultCellSize;
let actualCellSizeY: number = defaultCellSize;
const paddingPercentage = 0.05; // 5% padding on each side
const sampleImageSrc = './Images/santa.jpg'; // Updated image path

const pastelColors = [ // Pastel tone color palette
'#FFB6C1', '#FFF68F', '#98FB98', '#87CEEB'
];
const cellColors: string[][] = []; // 2D array to store the color of each cell

const img = new Image(); // Image object
img.src = sampleImageSrc;
const sampleImageAlpha = 0.7; // Fixed transparency for the sample image
```
```ts
img.onload = function () {
updateCanvas();
};
```
```ts
function updateCanvas() {
const numCols = Math.floor(window.innerWidth / defaultCellSize);
const numRows = Math.floor(window.innerHeight / defaultCellSize);
actualCellSizeX = window.innerWidth / numCols;
actualCellSizeY = window.innerHeight / numRows;

canvas.width = numCols * actualCellSizeX;
canvas.height = numRows * actualCellSizeY;

for (let y = 0; y < numRows; y += 1) {
const rowColors: string[] = [];

for (let x = 0; x < numCols; x += 1) {
// Assign color
if (firstRun) {
// Assign a random color
const randomColor = pastelColors[Math.floor(Math.random() * pastelColors.length)];
rowColors.push(randomColor);
ctx.fillStyle = randomColor;
} else {
ctx.fillStyle = cellColors[y][x];
}
ctx.globalAlpha = 1; // Do not apply transparency to the background color
ctx.fillRect(x * actualCellSizeX, y * actualCellSizeY, actualCellSizeX, actualCellSizeY);

// Draw the sample image in the grid (apply fixed transparency)
const paddingX = actualCellSizeX * paddingPercentage;
const paddingY = actualCellSizeY * paddingPercentage;

const resizedWidth = actualCellSizeX - 2 * paddingX;
const resizedHeight = actualCellSizeY - 2 * paddingY;

// Draw image with padding at the center of each cell
const imgX = x * actualCellSizeX + paddingX;
const imgY = y * actualCellSizeY + paddingY;
ctx.globalAlpha = sampleImageAlpha;
ctx.drawImage(img, imgX, imgY, resizedWidth, resizedHeight);
}

if (firstRun) {
cellColors.push(rowColors);
}
}
firstRun = false;
}
```
```ts
function handleInput(event: MouseEvent) {
const clickedX = event.clientX - canvas.getBoundingClientRect().left;
const clickedY = event.clientY - canvas.getBoundingClientRect().top;

const colIndex = Math.floor(clickedX / actualCellSizeX);
const rowIndex = Math.floor(clickedY / actualCellSizeY);

// Change the palette order each time you click
const currentColorIndex = pastelColors.findIndex(color => color === cellColors[rowIndex][colIndex]);
const nextColorIndex = (currentColorIndex + 1) % pastelColors.length;

// Only change the color of the selected cell
cellColors[rowIndex][colIndex] = pastelColors[nextColorIndex];

// Redraw only the selected cell
ctx.fillStyle = cellColors[rowIndex][colIndex];
ctx.globalAlpha = 1; // Do not apply transparency to the background color
ctx.fillRect(colIndex * actualCellSizeX, rowIndex * actualCellSizeY, actualCellSizeX, actualCellSizeY);

// Update the image
img.src = sampleImageSrc;

// // Log variables for debugging
// console.log('actualCellSizeX:', actualCellSizeX);
// console.log('actualCellSizeY:', actualCellSizeY);
// console.log('clickedX:', clickedX);
// console.log('clickedY:', clickedY);
// console.log('colIndex:', colIndex);
// console.log('rowIndex:', rowIndex);
// console.log('currentColorIndex:', currentColorIndex);
// console.log('nextColorIndex:', nextColorIndex);
// console.log('cellColors:', cellColors);
}
```
```ts
canvas.addEventListener('mousedown', handleInput);
canvas.addEventListener('touchstart', (event) => {
event.preventDefault();
handleInput(event);
}, { passive: true });

window.addEventListener('resize', () => {
firstRun = true;
updateCanvas();
});
```
</details>

- Sources
- `sample.png` : [Child Head With Smiling Face Icon (OnlineWebFonts.COM)](https://www.onlinewebfonts.com/icon/57082)
14 changes: 14 additions & 0 deletions TouchColorChangingTiles/TouchColorChangingTiles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* 2024.01.18 */

body, html {
background-color: #333;
margin: 0;
padding: 0;
overflow: hidden;
}

canvas {
display: block;
margin: auto;
}

19 changes: 19 additions & 0 deletions TouchColorChangingTiles/TouchColorChangingTiles.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- 2024.01.18 -->

<!DOCTYPE html>

<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="TouchColorChangingTiles.css">
<script defer src="TouchColorChangingTiles.js"></script>
<title>Touch-Color-Changing Tiles for My Son</title>
</head>

<body>
<canvas id="responsiveGridCanvas"></canvas>
</body>

</html>
94 changes: 94 additions & 0 deletions TouchColorChangingTiles/TouchColorChangingTiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 2024.01.18
var firstRun = true;
var canvas = document.getElementById('responsiveGridCanvas');
var ctx = canvas.getContext('2d');
var defaultCellSize = 120; // Default cell size
var actualCellSizeX = defaultCellSize;
var actualCellSizeY = defaultCellSize;
var paddingPercentage = 0.05; // 5% padding on each side
var sampleImageSrc = './Images/santa.jpg'; // Updated image path
var pastelColors = [
'#FFB6C1', '#FFF68F', '#98FB98', '#87CEEB'
];
var cellColors = []; // 2D array to store the color of each cell
var img = new Image(); // Image object
img.src = sampleImageSrc;
var sampleImageAlpha = 0.7; // Fixed transparency for the sample image
img.onload = function () {
updateCanvas();
};
function updateCanvas() {
var numCols = Math.floor(window.innerWidth / defaultCellSize);
var numRows = Math.floor(window.innerHeight / defaultCellSize);
actualCellSizeX = window.innerWidth / numCols;
actualCellSizeY = window.innerHeight / numRows;
canvas.width = numCols * actualCellSizeX;
canvas.height = numRows * actualCellSizeY;
for (var y = 0; y < numRows; y += 1) {
var rowColors = [];
for (var x = 0; x < numCols; x += 1) {
// Assign color
if (firstRun) {
// Assign a random color
var randomColor = pastelColors[Math.floor(Math.random() * pastelColors.length)];
rowColors.push(randomColor);
ctx.fillStyle = randomColor;
}
else {
ctx.fillStyle = cellColors[y][x];
}
ctx.globalAlpha = 1; // Do not apply transparency to the background color
ctx.fillRect(x * actualCellSizeX, y * actualCellSizeY, actualCellSizeX, actualCellSizeY);
// Draw the sample image in the grid (apply fixed transparency)
var paddingX = actualCellSizeX * paddingPercentage;
var paddingY = actualCellSizeY * paddingPercentage;
var resizedWidth = actualCellSizeX - 2 * paddingX;
var resizedHeight = actualCellSizeY - 2 * paddingY;
// Draw image with padding at the center of each cell
var imgX = x * actualCellSizeX + paddingX;
var imgY = y * actualCellSizeY + paddingY;
ctx.globalAlpha = sampleImageAlpha;
ctx.drawImage(img, imgX, imgY, resizedWidth, resizedHeight);
}
if (firstRun) {
cellColors.push(rowColors);
}
}
firstRun = false;
}
function handleInput(event) {
var clickedX = event.clientX - canvas.getBoundingClientRect().left;
var clickedY = event.clientY - canvas.getBoundingClientRect().top;
var colIndex = Math.floor(clickedX / actualCellSizeX);
var rowIndex = Math.floor(clickedY / actualCellSizeY);
// Change the palette order each time you click
var currentColorIndex = pastelColors.findIndex(function (color) { return color === cellColors[rowIndex][colIndex]; });
var nextColorIndex = (currentColorIndex + 1) % pastelColors.length;
// Only change the color of the selected cell
cellColors[rowIndex][colIndex] = pastelColors[nextColorIndex];
// Redraw only the selected cell
ctx.fillStyle = cellColors[rowIndex][colIndex];
ctx.globalAlpha = 1; // Do not apply transparency to the background color
ctx.fillRect(colIndex * actualCellSizeX, rowIndex * actualCellSizeY, actualCellSizeX, actualCellSizeY);
// Update the image
img.src = sampleImageSrc;
// // Log variables for debugging
// console.log('actualCellSizeX:', actualCellSizeX);
// console.log('actualCellSizeY:', actualCellSizeY);
// console.log('clickedX:', clickedX);
// console.log('clickedY:', clickedY);
// console.log('colIndex:', colIndex);
// console.log('rowIndex:', rowIndex);
// console.log('currentColorIndex:', currentColorIndex);
// console.log('nextColorIndex:', nextColorIndex);
// console.log('cellColors:', cellColors);
}
canvas.addEventListener('mousedown', handleInput);
canvas.addEventListener('touchstart', function (event) {
event.preventDefault();
handleInput(event);
}, { passive: true });
window.addEventListener('resize', function () {
firstRun = true;
updateCanvas();
});
Loading