Copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Anzu WebGL Example</title>
<style>
body { margin: 0; background: #000; }
canvas { display: block; }
</style>
</head>
<body>
<canvas id="webgl-canvas"></canvas>
<!-- Include Anzu SDK files -->
<script src="intrinsic-ads-js-sdk/core/SDKLoader.js"></script>
<script src="intrinsic-ads-js-sdk/utils/AnzuDebug.js"></script>
<script src="intrinsic-ads-js-sdk/utils/AnzuMath.js"></script>
<script src="intrinsic-ads-js-sdk/utils/UriSchema.js"></script>
<script src="intrinsic-ads-js-sdk/core/AnzuCore.js"></script>
<script src="intrinsic-ads-js-sdk/core/ChannelManager.js"></script>
<script src="intrinsic-ads-js-sdk/core/RenderManager.js"></script>
<script src="intrinsic-ads-js-sdk/AnzuAd.js"></script>
<script>
// WebGL setup
const canvas = document.getElementById('webgl-canvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl');
if (!gl) {
alert('WebGL not supported!');
}
// Vertex shader
const vertexShaderSource = `
attribute vec2 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
void main() {
gl_Position = vec4(a_position, 0.0, 1.0);
v_texCoord = a_texCoord;
}
`;
// Fragment shader
const fragmentShaderSource = `
precision mediump float;
uniform sampler2D u_texture;
varying vec2 v_texCoord;
void main() {
gl_FragColor = texture2D(u_texture, v_texCoord);
}
`;
// Compile shader
function compileShader(source, type) {
const shader = gl.createShader(type);
gl.shaderSource(shader, source);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
console.error('Shader compile error:', gl.getShaderInfoLog(shader));
gl.deleteShader(shader);
return null;
}
return shader;
}
// Create program
const vertexShader = compileShader(vertexShaderSource, gl.VERTEX_SHADER);
const fragmentShader = compileShader(fragmentShaderSource, gl.FRAGMENT_SHADER);
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
console.error('Program link error:', gl.getProgramInfoLog(program));
}
gl.useProgram(program);
// Create billboard geometry (quad)
const positions = new Float32Array([
-0.5, 0.28, // Top left
0.5, 0.28, // Top right
-0.5, -0.28, // Bottom left
0.5, -0.28 // Bottom right
]);
const texCoords = new Float32Array([
0, 0, // Top left
1, 0, // Top right
0, 1, // Bottom left
1, 1 // Bottom right
]);
// Position buffer
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, positions, gl.STATIC_DRAW);
const positionLocation = gl.getAttribLocation(program, 'a_position');
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
// Texture coordinate buffer
const texCoordBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, texCoords, gl.STATIC_DRAW);
const texCoordLocation = gl.getAttribLocation(program, 'a_texCoord');
gl.enableVertexAttribArray(texCoordLocation);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
// Create texture
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
// Initialize Anzu
AnzuCore.initialize({
appKey: "YOUR_ANZU_APP_KEY",
logLevel: 1
});
// Create billboard
const billboard = new AnzuAd({
channelName: "webgl_billboard",
aspectRatio: 16 / 9,
isDynamic: true,
isClickable: true
});
billboard.initialize();
// Handle clicks
canvas.addEventListener('click', () => {
billboard.interact();
});
// Game loop
let lastTime = performance.now();
function render(currentTime) {
const deltaTime = (currentTime - lastTime) / 1000;
lastTime = currentTime;
// Update Anzu
AnzuCore.update(deltaTime);
// Get ad canvas and update texture
const adCanvas = billboard.getCanvas();
if (adCanvas && adCanvas.width > 0) {
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, adCanvas);
}
// Clear and render
gl.clearColor(0.1, 0.1, 0.1, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
// Report visibility
billboard.updateVisibility({
angle: 0,
visibility: 1.0,
viewability: 1.0
});
requestAnimationFrame(render);
}
requestAnimationFrame(render);
</script>
</body>
</html>