2014-12-14 10 views
5

ho uno SVG con una viewBox 0 0 500 500 e una trasformazione matrix 0.8,0,0,0.8,54,54SVG - Calcolare una trasformazione di matrice a uno SVG con un viewBox diversa

ora voglio aggiungere questa trasformazione ad altri SVG. Il problema è che tutti gli altri SVG hanno una viewbox diversa. Così ho scritto una funzione per calcolare la trasformazione basata sulla viewbox ...

getAdjustedTransform('0.8,0,0,0.8,54,54','0 0 500 500','0 0 100 100'); 

funziona abbastanza bene.

Ma ora ho trovato un altro problema. Alcuni SVG hanno una viewbox con un'altezza e una larghezza diverse (non un sqaure).

cerco di risolvere questo problema qui

$viewboxWH_diff = $toThisViewBox_arr[2]/$toThisViewBox_arr[3]; 
$transform_arr_adjusted[5] = $transform_arr_adjusted[5]*$viewboxWH_diff; 

Ma il mio calcolo non è corretto. Qualche idea su cosa faccio di sbagliato?

function getAdjustedTransform($transform,$viewBox,$toThisViewBox) 
{ 
    $transform_arr  = explode(",", $transform);  // transform from the source SVG 
    $viewBox_arr  = explode(" ", $viewBox);  // viewbox from the source SVG 
    $toThisViewBox_arr = explode(" ", $toThisViewBox);    

    $transform_arr_adjusted = array(); 

    $val_1 = $transform_arr[4]/$viewBox_arr[2]; 
    $val_2 = $transform_arr[5]/$viewBox_arr[3]; 

    $transform_arr_adjusted[0] = $transform_arr[0]; 
    $transform_arr_adjusted[1] = $transform_arr[1]; 
    $transform_arr_adjusted[2] = $transform_arr[2];     
    $transform_arr_adjusted[3] = $transform_arr[3]; 
    $transform_arr_adjusted[4] = $val_1 * $toThisViewBox_arr[2]; 
    $transform_arr_adjusted[5] = $val_1 * $toThisViewBox_arr[3]; 

    // if viewbox with and height != 
    if($toThisViewBox_arr[2] != $toThisViewBox_arr[3]) 
    { 
     if($toThisViewBox_arr[2] > $toThisViewBox_arr[3]) 
     { 
      $viewboxWH_diff = $toThisViewBox_arr[2]/$toThisViewBox_arr[3]; 
      $transform_arr_adjusted[5] = $transform_arr_adjusted[5]*$viewboxWH_diff; 
     } 
     else 
     { 
      $viewboxWH_diff = $toThisViewBox_arr[3]/$toThisViewBox_arr[2]; 
      $transform_arr_adjusted[5] = $transform_arr_adjusted[5]*$viewboxWH_diff; 
     } 
    } 

    $transform_arr_adjusted = implode(',',$transform_arr_adjusted); 
    return $transform_arr_adjusted;  
} 



EDIT:
SVG risultati di questa funzione
http://jsfiddle.net/nw6ykszn/

+0

Sembra che si stia tentando di scrivere il proprio getCTM o getScreenCTM, perché non utilizzare invece quelle incorporate nelle funzioni SVG DOM? –

+0

Non sapevo che esistesse. Ma questa è solo una soluzione JS, giusto? – Peter

+0

Sì, questa è l'implementazione di Firefox (in C++) se si desidera implementarla: http://mxr.mozilla.org/mozilla-central/source/dom/svg/SVGContentUtils.cpp#380 in php –

risposta

0

intendevo mettere questo come un commento, ma la mia reputazione non mi consente.

Mi chiedo se è un errore di battitura o se l'errore è nel codice pure.

$transform_arr_adjusted[5] = $val_1 * $toThisViewBox_arr[3]; 

Credo che dovrebbe essere:

$transform_arr_adjusted[5] = $val_2 * $toThisViewBox_arr[3]; 

Speranza che aiuta!