فيديو المحاضرة
فيديو المحاضرة
بعد الفيديو: الأمثلة والاختبار والتمارين والمشروع الصغير تحت.
رسوم متحركة
أمثلة كود
أمثلة قصيرة وبسيطة
كل مثال وراه زرار «شغّل خطوة بخطوة»: بيعلّم على السطر اللي بيتنفّذ ويطبع الناتج زي ما بيتنفّذ بالظبط. ده شرح تنفيذ الكود سطر بسطر، مش مترجم حقيقي جوه الصفحة.
المثال ١: حجم الصورة في الذاكرة
const width = 4000, height = 3000;const bytes = width * height * 3; // 3 bytes per pixel: R, G, Bconst mb = bytes / (1024 * 1024);console.log(bytes);console.log(mb.toFixed(2) + " MB");
المثال ٢: كام لون في ٢٤ بت؟
const bitsPerPixel = 24;const perChannel = 2 ** (bitsPerPixel / 3);const colors = 2 ** bitsPerPixel;console.log(perChannel);console.log(colors);
المثال ٣: الطباعة بالـ ppi
const ppi = 300;const wInch = 4, hInch = 6;const wPx = ppi * wInch;const hPx = ppi * hInch;console.log(wPx + " x " + hPx);console.log(wPx * hPx);
اختبار قصير
اختبر فهمك
1. Who coined the term computer graphics, and when?
2. A bitmap (raster) image is made of:
3. Which one is a vector format?
4. Which one is a graphics language rather than an image format?
5. Image processing differs from computer graphics because it:
تمارين
جرّب بإيدك
التمرين 1
اعمل متغيرين width=1920 و height=1080، احسب عدد البكسلات، وبعدين حجم الفريم بالميجابايت لو كل بكسل 3 بايت (رقمين عشريين)، واطبع الاتنين.
Create width=1920 and height=1080, compute the pixel count, then the frame size in MB at 3 bytes per pixel (2 decimals), and print both.
Starter code:
const width = 1920, height = 1080;// TODO: pixels = width * height// TODO: mb = pixels * 3 / (1024 * 1024)// TODO: print pixels, then mb with 2 decimals + " MB"
const width = 1920, height = 1080; const pixels = width * height; const mb = pixels * 3 / (1024 * 1024); console.log(pixels); console.log(mb.toFixed(2) + " MB");
Expected output
2073600 5.93 MB
التمرين 2
اكتب دالة colors(bits) بترجّع 2 أس bits، يعني عدد الألوان الممكنة، واطبع colors(8) و colors(24).
Write colors(bits) returning 2 ** bits, the number of possible colors, and print colors(8) and colors(24).
Starter code:
function colors(bits) { // TODO: return 2 ** bits}console.log(colors(8));console.log(colors(24));
function colors(bits) {
return 2 ** bits;
}
console.log(colors(8));
console.log(colors(24));
Expected output
256 16777216
مشروع صغير في آخر المحاضرة
ارسم أيقونة قلب 8x8 بالبكسلات
الداتا: our own 8x8 grid of color indexes (1 = red pixel, 0 = background); no external data.
License / source: Our own grid; Canvas API reference by MDN (link only, nothing copied). Reference page
الخطوات
- خد شبكة 8 في 8، كل خلية رقم: ١ يعني بكسل أحمر وصفر يعني خلفية.
- لف على الصفوف والأعمدة، وكل خلية فيها ١ ارسمها مربع أحمر.
- عدّ البكسلات الحمرا واطبع العدد.
Starter code:
const grid = [ [0,1,1,0,0,1,1,0], [1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1], [0,1,1,1,1,1,1,0], [0,0,1,1,1,1,0,0], [0,0,0,1,1,0,0,0], [0,0,0,0,0,0,0,0]];// TODO: count the red pixels (the 1s) and print the grid size and the count
const grid = [
[0,1,1,0,0,1,1,0],
[1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1],
[1,1,1,1,1,1,1,1],
[0,1,1,1,1,1,1,0],
[0,0,1,1,1,1,0,0],
[0,0,0,1,1,0,0,0],
[0,0,0,0,0,0,0,0]];
const ctx = document.getElementById("miniCanvas").getContext("2d");
const S = 24;
let count = 0;
grid.forEach((row, y) => row.forEach((v, x) => {
if (v) { ctx.fillStyle = "#e0245e"; ctx.fillRect(x*S, y*S, S, S); count++; }
}));
console.log(grid.length + " x " + grid[0].length);
console.log(count);
Expected output
8 x 8 40
Credits: the topic order follows the course notes "Multimedia and Computer Graphics" by Dr. Muhammad Kashif Saeed (Applied College, Muhayil, King Khalid University), shared by the course owner. The explanations, diagrams, data and code in this lecture are our own; we copy nothing from the notes. Extra reference: Multimedia and Computer Graphics notes, Dr. Muhammad Kashif Saeed, King Khalid University (elc.kku.edu.sa) - own text, structure follows the notes.