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


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

imageMode()函數用於設置圖像的圖像模式。圖像模式通過更改解釋給image()函數的參數的方式來定義圖像在畫布中的位置。

用法:

imageMode( mode )

參數:該函數接受定義了要使用的模式的單個參數模式。它可以具有以下值:


  • CORNER:此模式將提供給image()的第二個和第三個參數解釋為圖像的左上角。
  • CORNERS:此模式將賦予image()的第二個和第三個參數解釋為圖像的左上角,將第四個和第五個參數解釋為圖像的右下角。
  • CENTER:此模式將指定給image()的第二個和第三個參數解釋為圖像的中心點。另外,如果指定了第四和第五個參數,它們將用作圖像的寬度和高度。

下麵的示例說明了p5.js中的imageMode()函數:

例:

function preload() { 
  img = loadImage('sample-image.png'); 
} 
  
function setup() { 
  imageModes = [ 
    CORNER, 
    CORNERS, 
    CENTER 
  ]; 
  i = 0; 
  currMode = imageModes[i]; 
  
  createCanvas(600, 400); 
  textSize(22); 
  
  // Create a button for the switching 
  // the imageMode 
  switchBtn = createButton("Change imageMode"); 
  switchBtn.position(30, 400) 
  switchBtn.mousePressed(switchMode); 
} 
  
function draw() { 
  clear(); 
  text("Click on the button change the current"+ 
                           " imageMode", 20, 20); 
  text("Current imageMode:" + currMode, 20, 40); 
  
  // Creating a rectangle to demonstrate 
  // the location of the image 
  rect(150, 150, 200, 200); 
  
  // Setting the imageMode 
  imageMode(currMode); 
  
  // Drawing the image 
  image(img, 150, 150, 200, 200); 
} 
  
function switchMode() { 
  // Change the current imageMode 
  if (i < imageModes.length - 1) 
    i++; 
  else
    i = 0; 
  currMode = imageModes[i]; 
}

輸出:

在線編輯:

環境設置:

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



相關用法


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