Use filters for Text in GPU mode AIR mobile

后端 未结 3 1138
深忆病人
深忆病人 2021-01-13 16:31

Unfortunately the filters do not work (drop shadow, glow) in GPU mode. I\'m looking for an opportunity to use these effects to text in this mode. I will welcome any advice.<

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-13 16:58

    As Astraport mentions, you'll need to draw the textfield out to a bitmapData every time you update the text using bitmapData.draw().

    If you use textField.getBounds to determine the size of the bitmapData you need, the resulting bounds rectangle will not include the extra size due to the filter (e.g. a DropShadowFilter sticks out the side of the textbox by certain pixels depending on the 'distance' and 'blur'). To ensure that you include the filters when you draw the bitmap, you'll also need to use bitmapData.generateFilterRect() to get the correct size rect.

    Code snippet (untested, but general idea):

    // Remember the transform matrix of the text field
    var offset : Matrix = myTextField.transform.matrix.clone();
    // Get the bounds of just the textfield (does not include filters)
    var tfBounds : Rectangle = myTextField.getBounds( myTextField.parent );     
    // Create a bitmapData that is used just to calculate the size of the filters
    var tempBD : BitmpaData = new BitmapData( Math.ceil(tfBounds.width), Math.ceil(tfBounds.height) );
    // Make a copy of the textField bounds. We'll adjust this with the filters
    var finalBounds : rectangle = tfBounds.clone();
    // Step through each filter in the textField and adjust our bounds to include them all
    var filterBounds : rectangle;
    for each (var filter : BitmapFilter in myTextField.filters) {
        filterBounds = tempBD.generateFilterRect( tfBounds, filter );
        finalBounds.left = Math.min( finalBounds.left, filterBounds.left );
        finalBounds.right = Math.max( finalBounds.right, filterBounds.right );
        finalBounds.top = Math.min( finalBounds.top, filterBounds.top );
        finalBounds.bottom = Math.max( finalBounds.bottom, filterBounds.bottom );
    }
    
    // Now draw the textfield to a new bitmpaData
    var textFieldBD : BitmpaData = new BitmapData( Math.ceil(finalBounds.width), math.ceil(finalBounds.height) );
    offset.tx = -finalBounds.x;
    offset.ty = -finalBounds.y;
    textFieldBD.draw( myTextField.parent, offset, myTextField.transform.colorTransform );
    
    // Create a bitmap and add the bitmap data. Note: normally you would create a
    // bitmap once and just update the bitmpaData
    var bitmap : Bitmap = new Bitmap();
    myTextField.parent.addChild( bitmap );
    
    // Position the bitmap in same place as textField
    bitmap.bitmapData = textFieldBD;
    bitmap.x = myTextField.x - finalBounds.x;
    bitmap.y = myTextField.y - finalBounds.y;
    myTextField.visible = false;
    

提交回复
热议问题