當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js pixelDensity()用法及代碼示例

p5.j​​s中的pixelDensity()函數用於設置高像素密度顯示器的像素比例。像素密度的默認值設置為與顯示密度匹配。 pixelDensity(1)用於關閉顯示密度。不帶參數的pixelDensity()函數將返回草圖的當前像素密度。

用法:

pixelDensity(c)

參數:該函數接受單個參數c,該參數存儲刻度值。


以下程序說明了p5.js中的pixelDensity()函數:

示例1:本示例使用pixelDensity()函數顯示像素密度。

function setup() { 
      
    // Create canvas of window size 
    createCanvas(windowWidth, windowHeight); 
      
    // Set Pixel Density to 9 
    pixelDensity(9); 
} 
   
function draw() { 
      
    // Set the background color 
    background(0, 200, 0); 
    
    color("green"); 
      
    // Set the text size 
    textSize(30); 
      
    // Set the text align 
    textAlign(CENTER); 
      
    // Display text on the screen 
    text("GeeksForGeeks!", windowWidth/2, 
                windowHeight/2); 
    text("PixelDensity is " + pixelDensity(), 
         windowWidth/2, windowHeight/2 + 40); 
   
}

輸出:

示例2:本示例使用pixelDensity()函數顯示像素密度。

function setup() { 
      
    // Create canvas of window size 
    createCanvas(windowWidth, windowHeight); 
      
    // Set Pixel Density to 9 
    pixelDensity(3); 
} 
   
function draw() { 
      
    // Set the background color 
    background(160, 200, 50); 
      
    // Set the text size 
    textSize(30); 
      
    // Set the text align 
    textAlign(CENTER); 
      
    // Display text on the screen 
    text("GeeksForGeeks!", windowWidth/2, 
                windowHeight/2); 
    text("PixelDensity is " + pixelDensity(), 
         windowWidth/2, windowHeight/2 + 40); 
   
}

輸出:

參考: https://p5js.org/reference/#/p5/pixelDensity



相關用法


注:本文由純淨天空篩選整理自sarthak_ishu11大神的英文原創作品 p5.js | pixelDensity() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。