css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <canvas width="512" height="512"></canvas>
            
          
!
            
              const WIDTH = 512;
const HEIGHT = 512;
const N = 64;

// 光源的中心位置
let sourceX = 0.5;
let sourceY = 0.5;

const p = [];
for (let y = 0, i = 0; y < HEIGHT; y++) {
    for (let x = 0; x < WIDTH; x++) {
        // x / W, y / H 后其值被限制在 [0, 1] 之间
        p[i++] = Math.floor(Math.min(sample(x / WIDTH, y / HEIGHT) * 255, 255));
    }
}

function sample(x, y) {
    let sum = 0;
    for (let i = 0; i < N; i++) {
        // 以下为三种不同的采样方式
        // const theta = Math.PI * 2 * Math.random();           // 随机采样
        // const theta = Math.PI * 2 * i / N;                   // 分层采样(stratified sampling)
        const theta = Math.PI * 2 * (i + Math.random()) / N;    // 抖动采样(jittered sampling)

        // trace() 所返回的值是点 (x, y) 从 theta 方向获取的光
        sum += trace(x, y, Math.cos(theta), Math.sin(theta));
    }
    return sum / N;
}

function circleSDF(x, y, cx, cy, r) {
    const ux = x - cx;
    const uy = y - cy;
    return Math.sqrt(ux * ux + uy * uy) - r;
}

function trace(ox, oy, dx, dy) {
    const MAX_STEP = 10;
    const MAX_DISTANCE = 2;
    const EPSILON = 1e-6;

    let t = 0.0;    // t 为步进的距离
    for (let i = 0; i < MAX_STEP && t < MAX_DISTANCE; i++) {
        // 光源中心为 (sourceX, sourceY) 
        // 沿单位向量 (dx, dy) 方向前进,t 表示前进的距离
        const sd = circleSDF(ox + dx * t, oy + dy * t, sourceX, sourceY, 0.1);

        // 此时已到达发光的圆形表面
        if (sd < EPSILON) {
            return 2.0;
        }
        // 继续增加步进的距离
        t += sd;
    }
    return 0.0;
}

const canvas = document.getElementsByTagName('canvas')[0];
const ctx = canvas.getContext('2d');
const imageData = ctx.getImageData(0, 0, WIDTH, HEIGHT);
const data = imageData;

processImageData(imageData, p);
ctx.putImageData(imageData, 0, 0);

function processImageData(imageData, p) {
    const data = imageData.data;
    for (let i = 0; i < data.length; i += 4) {
        const value = p[i / 4];
        data[i] = value;
        data[i + 1] = value;
        data[i + 2] = value;
        data[i + 3] = 255;
    }
}

function mouseMoveHandler(e) {
    const rect = canvas.getBoundingClientRect();
    console.log('点击位置', e.clientX, e.clientY);

    const x = e.clientX - rect.left;
    const y = e.clientY - rect.top;

    // 改变光源位置
    sourceX = x / 512;
    sourceY = y / 512;

    for (let y = 0, i = 0; y < HEIGHT; y++) {
        for (let x = 0; x < WIDTH; x++) {
            p[i++] = Math.floor(Math.min(sample(x / WIDTH, y / HEIGHT) * 255.0, 255.0));
        }
    }
    const imageData = ctx.getImageData(0, 0, WIDTH, HEIGHT);
    processImageData(imageData, p);
    ctx.putImageData(imageData, 0, 0);
}

// 加上以下点击事件后,可改变光源位置
canvas.addEventListener('click', mouseMoveHandler);

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console